- Home /
How can I Vector3 Slerp or Lerp locally?
Hello, I'm using:
if(forwardMoveSpeed > 0){
this.transform.localPosition = Vector3.Lerp(this.transform.position , new Vector3(this.transform.position.x + 1f,this.transform.position.y, this.transform.position.z), forwardMoveSpeed * Time.deltaTime);
forwardMoveSpeed -= 0.05f;
}
to move my character for a small amount of time. But this moves it in the global x position. As soon as my character rotates it doesn't look right, obviously.
How can I move my character in it's local x position.
Thanks.
I did some searches and found a bunch on how to rotate something locally/globally but nothing about just moving using Vector3.
EDIT:
So let me explain a tad bit more so you can get a better understanding.
I have a character(GameObject). He runs a path and has a specific amount of life. When he dies I want him to fall "forward" while doing his dying animation.
He faces the Z axis, therefore "forward" is his LOCAL Z axis. The path he travels has 90 degree turns. So he faces different directions relative to the global axis. Sometimes his LOCAL z axis is facing the GLOBAL x axis, sometimes the GLOBAL y Axis.
With that being said all I want to do is move his overall position's LOCAL z axis by like 2 units while he's doing his animation. (so if his position is 0,0,0 then when he dies I want his LOCAL position to be 0,0,2. I can't just change his position because his LOCAL Z axis might be the GLOBALS X axis).
How do I do that?
Eveything I try moves him 2 units in a GLOBAL axis.
Answer by trs9556 · May 28, 2013 at 12:14 AM
Ok so I couldn't find a solution so I just made my own personal converter. This code is pretty specific to my model but if your "forward" is your local Z axis then it might work for you.
Thanks to everyone who tried to help, maybe I didn't explain it good enough.
if(forwardMoveSpeed > 0){
/*@@@@LEGEND
*In a perfect world, if all rotations were EXACT then this is what way the character is facing
*
*
* If it's y rotation is 90 degrees: He is facing X direction and forward is +x
*
* If it's y rotation is 0 degrees: He is facing Z direction and forward is +Z
*
* If it's y rotation is 180 degrees: He is facing Z direction and forward is -z
*
* If it's y rotation is 270 degrees: He is facing X direciont and forward is -x
*
*/
currentPosition = this.transform.position;
float moveAmount = 2f;
if(oneShot){
float currentRotitation = this.transform.rotation.eulerAngles.y;
//ALL VALUES ARE A RANGE/ABOUT BECAUSE WE CAN'T GAURENTEE THE CHARACTER'S ROTATION WILL BE EXACTLY 0,90,180, OR 270
//if rotoation is greater then 20 then it CANT be "0" rotation
if(currentRotitation > 20){
//if rotation is greater then 110 then it CANT be "90" rotation
if(currentRotitation > 110){
//if rotation is greater then 200 then it CANT be "180" rotation
if(currentRotitation > 200){
//if we are greater then 200 we have to be at "270" degrees
//forward is negative x
nextPosition = new Vector3(currentPosition.x - moveAmount, currentPosition.y, currentPosition.z);
}
//if it's less then 200 then it has to be "180" degrees
else{
//forward is negative z
nextPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z - moveAmount);
}
}
//if it's less then 110 it has to be "90" rotation
else{
//forward is positive x
nextPosition = new Vector3(currentPosition.x + moveAmount, currentPosition.y, currentPosition.z);
}
}
//if it's less then 20 it must be "0" rotation
else{
//forward is positive Z
nextPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z + moveAmount);
}
//this is so we only figure out our "end" location once.
oneShot = false;
}
this.transform.position = Vector3.Slerp(currentPosition , nextPosition, forwardMoveSpeed * Time.deltaTime);
forwardMoveSpeed -= 0.05f;
}
If you are moving in the forward direction of your object, and if you are facing the direction you want to go, then you can boil your code down to this:
if(forward$$anonymous$$oveSpeed > 0){
currentPosition = this.transform.position;
float moveAmount = 2f;
if(oneShot){
nextPosition = transform.forward * moveAmount;
oneShot = false;
}
this.transform.position = Vector3.Lerp(currentPosition , nextPosition, forward$$anonymous$$oveSpeed * Time.deltaTime);
forward$$anonymous$$oveSpeed -= 0.05f;
}
You want Lerp, not Slerp. Slerp is for spherical movement of vectors.
Actually you can get rid of your forward$$anonymous$$oveSpeed and use a constant speed (and still get an eased movement):
if(transform.position != nextPosition){
currentPosition = this.transform.position;
float moveAmount = 2f;
if(oneShot){
nextPosition = transform.forward * moveAmount;
oneShot = false;
}
this.transform.position = Vector3.Lerp(transform.position , nextPosition, speed * Time.deltaTime);
}
Answer by hiddenspring81 · May 27, 2013 at 11:11 PM
if you want to move the character in local-space, shouldn't you be using Lerp
on the transform.localposition
and not transform.position
? Try this instead
if(forwardMoveSpeed > 0){
this.transform.localPosition = Vector3.Lerp(this.transform.localposition , new Vector3(this.transform.localposition.x + 1f,this.transform.localposition.y, this.transform.localposition.z), forwardMoveSpeed * Time.deltaTime);
forwardMoveSpeed -= 0.05f;
}
I tried something similar while waiting for a response, and it did nothing. So I went to the docks and it makes sense why it doesn't work.
"Position of the transform relative to the parent transform." $$anonymous$$y script is on the parent so it just returns the same as transform.Position. Thanks though.
Answer by robertbu · May 27, 2013 at 11:16 PM
I'm not sure exactly what you to happen here. Typically you would have something like this:
var v3LocalDest : Vector3;
var speed = 2.0;
function Start() {
v3LocalDest = transform.localPosition;
v3LocalDest.x += 1.0;
}
function Update() {
transform.localPosition = Vector3.Lerp(transform.localPosition, v3LocalDest, Time.deltaTime * speed);
}
Note if you have a global position and want to set v3LocalDest, you can do:
var v3LocalDest = transform.InverseTransformPoint(v3GlobalPoint);
I've tried a few different ways using InverseTransformPoint and using TransformPoint, it all gives me weird cords. Same with TransformDirection
Based on your comment above, if this is on a parent object, replace all 'localPosition' with 'position' in this sample code.
Yes you are correct, I can use both localPosition or position sense they will return the same thing.
With that being said it does exactly what I already have accomplished. $$anonymous$$oves the GameObject in the global position. I'm looking for local, thanks though.