- Home /
When I rotate my player in open 3 space, the transform rotation component keeps snapping back to origin. However, when I disable my rigidbody.rotate command, the transform rotation works just fine.
Here is my code:
var speed is defined by the inspector.
void FixedUpdate() {
float moveHorizontal = Input.GetAxis( "Horizontal" );
float moveFoward = Input.GetAxis( "Vertical" );
float moveVertical = Input.GetAxis( "Jump" );
float rotatePlayerYaw = Input.GetAxis( "Mouse Y" );
float rotatePlayerPitch = Input.GetAxis( "Mouse X" );
Vector3 rotation = new Vector3( -rotatePlayerYaw, rotatePlayerPitch, 0.0f );
Vector3 movement = new Vector3( moveHorizontal, -moveVertical, moveFoward);
rigidbody.velocity = movement * speed;
// for debugging purposes
//if( Input.GetKeyDown( "j" ) )
//{
// transform.Rotate( Vector3.up * speed * Time.deltaTime );
// Debug.Log( "Key 'j' has been pressed" );
// Debug.Log( transform.eulerAngles );
//}
boundary.xMin = GameObject.Find( "Wall X Negative" ).transform.position.x + 15f;
boundary.xMax = GameObject.Find( "Wall X Positive" ).transform.position.x - 15f;
boundary.zMin = GameObject.Find( "Wall Z Negative" ).transform.position.z + 15f;
boundary.zMax = GameObject.Find( "Wall Z Positive" ).transform.position.z - 15f;
rigidbody.position = new Vector3
(
Mathf.Clamp( rigidbody.position.x, boundary.xMin, boundary.xMax ),
Mathf.Clamp( rigidbody.position.y, boundary.yMin, boundary.yMax ),
Mathf.Clamp( rigidbody.position.z, boundary.zMin, boundary.zMax )
);
rigidbody.rotation = Quaternion.Euler( rigidbody.velocity.z * tilt, 0.0f, rigidbody.velocity.x * -tilt * 1.3f );
transform.Rotate( rotation, Time.fixedDeltaTime * speed * 3.0f, Space.Self );
}
Comment