简体   繁体   中英

Don't rotate z-axis while rotating around Vector3.up and Vector3.left

I have a Space Shuttle that i want to rotate on Vector3.up and Vector3.left , so that the player can look up/down and left/right with the Shuttle.

This is the code i use:

// Rotate the ship
transform.Rotate(Vector3.up, distX * rotateSpeed * Time.deltaTime, Space.Self);
transform.Rotate(Vector3.left, distY * rotateSpeed * Time.deltaTime, Space.Self);

where distX is a value for the strength of the rotation, based on Mouse X - Axis and rotateSpeed is just a value for the speed of the rotation.

However, if i use this script to rotate my ship, it gets rotated by the z-axis, too.

And i have no idea why it's doing this. These are the only lines in my code (yet) that do something with the rotation of the Shuttle.

What i tried yet is to replace Vector3.up by transform.up and Vector3.left by transform.right (also changed distY to -distY in the second case), but didn't work either.

You can assign Euler value of 0 to z like which allows you to restrict rotation in z axis :

    distX = Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime;
    distY = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;       
    transform.rotation = Quaternion.Euler(distX, distY, 0f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM