Download

move an Actor via code

Hi,
I hate to ask such a personal question, but why does GetTransfrom.SetLocation() do nothing when called directly? I am using the following code to move my actor every frame:



void AMyTower::Tick(float DeltaTime) // same as Update in Unity
{
	FTransform tr = GetTransform();
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::SanitizeFloat(Speed*DeltaTime));
	GetTransform().SetLocation(tr.GetLocation() + FVector(Speed*DeltaTime, 0, 0));
	Super::Tick(DeltaTime); 
}

The Debug Message tells me that Tick is excecuted and that Speed*DeltaTime != null. Yet nothing changes in gameMode. When I print just the transform location while moving the actor manually it does change, so that means that I have the right transform right?

Any help would be appreciated. I just started using UE4 and already I’m stumped :S
Thanks!

GetTransform() gives you a copy of the actor’s transform, not a direct reference to it!

So you are editing returned data that is no longer connected to the actor.

You would have to call SetActorTransform() to get your method to work


FTransform ModifiedTrans = GetTransform();
ModifiedTrans.SetLocation(NewLoc);
SetActorTransform(ModifiedTrans);

You could just use SetActorLocation() :slight_smile:

Hi my GetTransform() gets underlined red and is not identified. How do i sort that out?

Please don’t bump a 3 year old thread, just start a new one.

The “red underline” is Intellisense throwing an error. These aren’t real compile errors, just parsing failures. You can safely ignore them 99% of the time (or install Visual Assist which replaces Intellisense and works much better).

If you are getting actual compile errors from that line, then you are likely missing an include for the actor/component. Just find where the actor/component is declared and add that file as a #include.