How to move a Pawn programatically

Hi,

I am trying to move the Pawn programatically. I have subclassed a SpectatorPawn like this:



// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "GameFramework/SpectatorPawn.h"
#include "CollapsePawn.generated.h"

/**
 * 
 */
UCLASS()
class ACollapsePawn : public ASpectatorPawn
{
	GENERATED_UCLASS_BODY()

	virtual void BeginPlay() OVERRIDE;
	
};


And in the implementation file I have this:



// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "Collapse.h"
#include "CollapsePawn.h"


ACollapsePawn::ACollapsePawn(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
    
}

void ACollapsePawn::BeginPlay()
{
    Super::BeginPlay();
    
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We are using CollapsePawn!"));
        
        if (Controller != NULL)
        {
            // find out which way is forward
            FRotator Rotation = Controller->GetControlRotation();

            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
            AddMovementInput(Direction, 100.0f);
            //MoveForward(1.0f);
        }
    }
}


Now regardless if I am using ‘AddMovementImput’ or just ‘MoveForward’ the Pawn is not moving.

What I am trying to achieve is the Pawn should accelerate constantly in forward direction unless he will hit some obstacle. This code should prove that I can move the pawn programatically but obviously it is not happening. Any help?

The input from AddMovementInput() is accumulated over the course of a frame and then processed when the MovementComponent ticks, but it is then “consumed” or cleared, making ready for the next frame of input. The Acceleration member of MovementComponent works in a similar way (since it is computed from the InputVector anyway). This is why if you look at the templates that move characters with the keyboard, we only need to respond to key events to add input, and not worry about clearing the movement input when a key is released. It will be cleared once it is processed and consumed.

If you change your pawn to add the input in Tick() it should work*.

*Ideally you would actually do it in the movement component TickComponent() function instead, just to avoid adding a frame of latency if you start to dynamically change the input. This is because the MovementComponent ticks before the Pawn each frame, and you would be doing it a frame late by doing it in the Pawn tick instead of the component. In the case you described it probably won’t matter though if you are consistently just moving forward.

Thanks ,

I will try that later today.