Problems with the axes of the object

Hello! Sorry for my english im from ukraine, is the object that you want to turn on its axis, but the axis is slightly angled. I object turns like this: 1.
But we need to properly scrolled =) like so: 2.Here is my script for the scroll object:

public class RevolverScript : MonoBehaviour {
        private float rotate_y ;
        public float spin_speed = 100;

        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {

                if (Input.GetKey ("left")) {
                        print("left");
                        rotate_y +=(spin_speed * Time.deltaTime);
                }

                if (Input.GetKey ("right")) {
                        print ("right");
                        rotate_y -=(spin_speed * Time.deltaTime);
                }
                rotate_y = rotate_y%360.0f;
                this.transform.rotation = Quaternion.Euler(0, rotate_y, 0);


        }

I tried to edit the pivot and still trying to shove my object in perfab. But this is not all useful, I do not know what to do. It seems to me that it does not really do. Please help as many races have tried to make himself but without success.3 for downloat this project.rar

1 Answer

1

You are using this.transform.rotation this is giving you the rotation of your gameobject using the world coordinates rotation.

you need to use Transform.localRotation in order to get the rotation axis relative to your gameobject.

so a working code should be like :

public class RevolverScript : MonoBehaviour {
        private float rotate_y ;
        public float spin_speed = 100;
 
        // Use this for initialization
        void Start () {
 
        }
 
        // Update is called once per frame
        void Update () {
 
                if (Input.GetKey ("left")) {
                        print("left");
                        rotate_y +=(spin_speed * Time.deltaTime);
                }
 
                if (Input.GetKey ("right")) {
                        print ("right");
                        rotate_y -=(spin_speed * Time.deltaTime);
                }
                rotate_y = rotate_y%360.0f;
                this.transform.localRotation = Quaternion.Euler(0, rotate_y, 0);
 
 
        }

thenks for answer, but its not work. I think can also try to use RotateAround, but also that it does not work: this.transform.RotateAround(cube.transform.position, Vector3.up, 90 * Time.deltaTime); Maybe I'm doing something wrong with the coordinates?

–

it helped me: this.transform.Rotate(Vector3.up, Time.deltaTime * 90);

–