Local Location, Can't change in C++

Hi all,

Note - Sorry for not putting this in the C++ section but it won’t allow me yet.

I’ve just finished an Unreal course but I’m still learning obviously. I’m currently trying to make a rectangle move based on a % of it’s size. That is working fine however if I want to rotate the shape in my level design I can’t. I always want to add the additional location to the Z axis. I’ve been trying for a while to find methods to translate or add vectors in the local space but I can’t seem to work it out.

void ACubeRed::TransformPositive() {
	FVector CurrentPosition = this->GetActorTransform().GetLocation();
	//UE_LOG(LogTemp, Warning, TEXT("Location is: %s"), *CurrentPosition.ToString());
	if (IncrementsMoved < 3) {
		float ObjectScale = this->GetActorScale().Z;
		CurrentPosition = CurrentPosition + FVector(0, 0, (200 / 3));
		this->SetActorLocation(CurrentPosition);
		//UE_LOG(LogTemp, Warning, TEXT("Called with new location %s"), *CurrentPosition.ToString());
		IncrementsMoved++;
	}
}

This is what I have so far. However I’ve also tried:

this->AddActorLocalOffset()

However this also doesn’t work. Can anyone enlighten me to how to convert my CurrentPosition (which is in the world system) into a local direction?

If so this would be really helpful.

Thanks for your time.

Harry

Could you specify your problem a little? I’m a little confused because you are talking about moving an object first, but then about rotating it?

I managed to do this by using scene components and lerping between them. What I meant was if I rotated an object in the world then it’s global and locaal axis are different. I’d want to translate the direction based on the local axis and this was only doing it by the world axis.

I think I get what you intend to do now. You want to move your Actor on its local Z-axis, right?
If so, here’s how I would do it:

void AMyActor::MoveAlongLocalZAxis(float Distance) {
    
    	FVector CurrentPosition = GetActorLocation(); // the current actor location
    	FVector UpVector = GetActorUpVector();	// up vector (Z) from this actor in world space (length 1.0)
    	FVector OffsetVector = UpVector * Distance; // the vector that needs to be added to your current position
    
    	SetActorLocation(CurrentPosition + OffsetVector);	
}

You can do the same with the X and Y axis if you use:

GetActorForwardVector() for the X-Axis

GetActorRightVector() for the Y axis

I hope this will help you. If you have any questions, let me know :slight_smile:

Thank you for taking the time to answer. So I presume this is based on the actors Local forward, up and right vector? I’ve managed to do this in blueprint now but this is great knowledge for the future if so.

One final question before I mark it as complete. To make this translate I imagine I’d use FMath:Lerp inside of the tick with some condition to make it “transition” rather than “teleport” right?

Thanks again.

SetActorLocation() works with, as you found out in the beginning, world coordinates. GetActorForwardVector() returns the, as you would say, local forward vector of the actor, but it actually gives it to you as a vector in world space, meaning relatively to the world. This is why you can use it together with SetActorLocation. I hope you understand what I mean, it’s a little hard for me to explain it :wink:

If you want it to transition smoothly, you should call MoveAlongLocalZAxis() each tick, passing small distance values. You could pass let’s say 1.0f each tick and stop when it has moved a certain distance. You don’t really need FMath::Lerp for that.
If you want it to transition over a certain time, you might take a look at how to use DeltaTime, which basically gives you the time that elapsed since the last tick (in seconds).

If you want to use blueprints, take a look at timelines. They basically interpolate between two values over a given time. They are easy to handle once you’ve understood how they work. I code most of what I do in UE, but it seems to me some things, especially simple animations, just are created faster and less complex with blueprints. So that’s probably what I would use in your case.

Maybe this tutorial will help you to make a few things clearer.

I hope I didn’t confuse you or anything with this information. If you have any further questions let me know!

You haven’t I have used Unity quite frequently in the past but it being a script only Engine I was getting a bit mixed up with different terminology such as world and local space etc. I also have used to the timeline with lerps at the moment but I’ll do what you suggested about calling it each tick with a small increment and multiplying to by the world DeltaTime to keep it frame rate independent.

Thanks so much for your answer though and I’m sure it will really help others. Great assistance!