Hi everyone,
I’m probably facing some rounding error here while rotating an object, I don’t know, hope someone can see the flaw in my code:
In my little side scroller I’m turning my little submarine left and right, 0 and 180 degrees on Y axis and then apply a directional force to move it forward (and up to prevent sinking). problem is that after some time I end up with a z-offset, (despite freezing z position as well as y+z rotation!) causing the boat to leave the level contraints.
// Submarine controls
if (Input.GetAxis('Horizontal') > 0) //right key: set direction and add forces
{
boatDirection = 0;
rigidbody.AddForce(fwForce*Time.timeScale,0,0);
rigidbody.AddForce(0,upForce*Time.timeScale,0);
}
if (Input.GetAxis('Horizontal') < 0) //left key: set direction and add forces
{
boatDirection = -180;
rigidbody.AddForce(-fwForce*Time.timeScale,0,0);
rigidbody.AddForce(0,upForce*Time.timeScale,0);
}
// rotate submarine - Dampen towards the target rotation
var target = Quaternion.Euler (0,boatDirection,0);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * turnSpeed);
can anybody tell me why freezing isn’t preventing the z-offset from happening?
cheers