- Home /
transform position y changes when character rotates
Whenever my character rotates towards the camera the players transform y value will jump around like crazy?
void Update () {
var charRotation = cam.transform.rotation;
charRotation.x = 0;
charRotation.z = 0;
//Rotate only when character moves
if(Input.GetAxis("Vertical") > 0 || Input.GetAxis("Horizontal") > 0)
transform.rotation = Quaternion.Slerp(transform.rotation, charRotation, Time.deltaTime * 2f);
}
Camera C#
void Start()
{
offset = new Vector3(player.position.x, player.position.y + 2f, player.position.z + 5f);
}
void Update()
{
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
var lookPos = player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 100f);
}
Comment