I'm using transform.translate()
to move an object around the world space, relative to the camera that is following it. The camera has an angle of 23 degrees on the X axis.
The problem is that when I move forward the object tries to go into the ground and when going backward the object starts jumping because of the angle of the camera. What I want to do is freeze the Y axis of the object However if I did this the object would no longer be able to jump.
The other solution to this would be to write a method which rests the object to the ground when not jumping but them if the object was jumping and going backwards it would go flying backwards again.
How do I solve this issue?
Answer
Figured it out. Essentially i'm taking the position of the object before and after then taking the difference of both of then then adding it back into the position, so that the difference in Y-axis position that is introduced from the camera transform.Translate() is compensated for.
//compensate for the rotation of the camera while moving
void CompensateTransform(float X, float Y)
{
float diffrence = transform.position.y;
transform.Translate(speed * Time.deltaTime * X, 0,
Time.deltaTime * Y * speed, camera.transform);
diffrence -= transform.position.y;
transform.position = new Vector3(transform.position.x,
transform.position.y + diffrence,
transform.position.z);
}
No comments:
Post a Comment