An Alternative Solution

if (HasAuthority())
{
	//                    B                            -         A
	//             -----------------------------------   -----------------
	// Direction = TargetLocation + GetActorLocation() - GetActorLocation()
	FVector Direction = TargetLocation.GetSafeNormal();
	FVector DistanceOffset = Direction * Speed * DeltaSeconds;
	SetActorLocation(GetActorLocation() + DistanceOffset);
}

TargetLocation is in local space (relative to the moving platform). To move it into world space we can add the moving platform’s location.

But since we’re calculating B - A for direction, the + GetActorLocation() and - GetActorLocation() cancel out, and we can simply use a normalized TargetLocation as our local space direction.

Scale it by speed and delta time and add it to GetActorLocation() to get the result.

1 Like

Excellent work. Thank you for taking the time to share this with others.

Yes, I came to the same conclusion that you just needed to add the normalized TargetLocation but scaled to get the usual frame interval vector.

This is partially incorrect because this does not account for any rotation and scaling applied to the platform.

[Edit to add details/clarifications]
The proposed code will only work with the assumption that the platform will never be rotated or scaled when placed in the map, which happens to be true in this lecture, but rarely is when making full games.

With rotation and scaling (well, non-uniform scaling, i.e. scaling that doesn’t affect all 3 axis by the same amount), the direction values (angles) change when converting between Local and World space, i.e. TargetLocation must be transform in some way before being used in world space coordinates (SetActorLocation).

But it is true that one can skip the whole “actor position” thing. Instead of calling TransportPosition, which also adds that actor world position, which then needs to be subtracted again as in the lecture, one can call TransportVector, which only accounts for the rotation and scaling, i.e. the parts that can change the direction:

if (HasAuthority())
{
	FVector GlobalTargetLocation = GetTransform().TransformVector(TargetLocation);
	FVector Direction = GlobalTargetLocation.GetSafeNormal();
	FVector DistanceOffset = Direction * Speed * DeltaSeconds;
	SetActorLocation(GetActorLocation() + DistanceOffset);
}

You’re commenting on posts from over 4 years ago Nador.

As for being incorrect, it actually isn’t. He was sharing his work which generally students were asked to, and the solution provided is correct for the lecture.

I wish to therefore explain why his answer is correct.
First, the world location i.e. initial platform location is unaffected by scale. The direction of movement is also unaffected by scale - it is using a safe normal or unit vector. The platform is set to move in the direction of the target and does not stop. This is the solution to this lecture.

Later lectures move to the end point and back while the platform is triggered by entering a trigger volume. I can confirm this actually works exactly as expected using the information above, even if a scale and rotation is applied and uses the scaled/rotated end-point as well relative to the platform.

The final result is the platform moves towards the correct location and ends at the correct target location before returning to the starting location.

I hope this clarifies @pdoogs answer.

Privacy & Terms