Hi guys,
There are any way to know the moviment direction of any char.
like that
Vector3 oldPos;
String moviment="noMoviment";
void Start(){
oldPos = transform.position;
}
void Update(){
// using oldpos andtransform.position
////code for if's
///code
if(...)
moviment="front";
if(...)
moviment="back";
if(...)
moviment="left";
if(...)
moviment="right";
if(...)
moviment="noMoviment";
}
oldPos=transform.position;
}
but i dont’t want to use input.getaxis(‘vertical’) neither (‘horizontal’)
I try to compare olpos.z and transform.position.z but when I rotate char there’s a bug
1 Answer
1
If you are looking for the movement relative to the character you can compare a the movement direction the four local directions in world space. First get the movement vector.
Vector3 v3 = transform.position - oldPos;
Then you have to compare that vector against the four movements: transform.forward, -transform.forward, transform.right, and -transform.right.
You can make the comparison using either Vector3.Angle() and find the direction with the lowest angle, or you can use Vector3.Dot() and find the pair that has the highest value.
If you are looking for movement with respect to global space, then you will use Vector3.left, Vector.right, Vector3.forward, and Vector3.back.
Note you will want to test to make sure oldPos and transform.position are not equal before doing the calculation. If they are equal, then the character has not moved.