-1
\$\begingroup\$

I am trying to have a smooth rotation animation on this hover-car to make it seem more realistic.

I am trying to code it so that if the key a or d is pressed it will rotate to the left and right accordingly. If it rotates too far then it dies and the scene is reloaded. When the keys a or d are lifted then it will smoothly revert back to its original position. I want it to rotate on the x axis of its origin. If you see in the image the scenes axis is z and the axis of origin that points in the direction of that from the vehicle is its x.

Here is the scene with the axis's of the vehicle: enter image description here

        public int speed = 100;
        public float maxrotation = 30f; //To Set Max Rotation

        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            if (transform.rotation.eulerAngles.x > 0)
            {
               
                transform.rotation.eulerAngles.x - 1; //there is an error on this line saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
                
            }
            if (transform.rotation.eulerAngles.x < 0)
            {
               
                transform.rotation.eulerAngles.x + 1; //there is an error on this line saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
                
            }
            if (Input.GetKey(KeyCode.A))
            {
                //Set a rate at which we should turn
                float turnSpeed = speed * Time.deltaTime;
                //Connect turning rate to horizonal motion for smooth transition
                float rotate = Input.GetAxis("Horizontal") * turnSpeed;
                //            //Get current rotation
                //            float currentRotation = gameObject.transform.rotation.z;
                //            //Add current rotation to rotation rate to get new rotation
                //            Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
                //            //Move object to new rotation
                //            gameObject.transform.rotation = rotation;
                gameObject.transform.Rotate(Vector3.left * rotate);
            }
            if (Input.GetKey(KeyCode.D))
            {
                //Set a rate at which we should turn
                float turnSpeed = speed * Time.deltaTime;
                //Connect turning rate to horizonal motion for smooth transition
                float rotate = Input.GetAxis("Horizontal") * turnSpeed;
                //            //Get current rotation
                //            float currentRotation = gameObject.transform.rotation.z;
                //            //Add current rotation to rotation rate to get new rotation
                //            Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
                //            //Move object to new rotation
                //            gameObject.transform.rotation = rotation;
                gameObject.transform.Rotate(Vector3.left * rotate);
                
            }
            if (transform.eulerAngles.x > maxrotation)
            {
                Application.LoadLevel(Application.loadedLevel); //kills player and reloads scene
                scorecript.scoreValue = 0; // sets score to zero
            }
            if (transform.eulerAngles.x < maxrotation - (maxrotation * 2))
            {
                Application.LoadLevel(Application.loadedLevel); //kills player and reloads scene
                scorecript.scoreValue = 0; //sets score to zero
            }

        }

<!-- end snippet -->

\$\endgroup\$
0
0
\$\begingroup\$
// Cache the rotation we had at start-up.
Quaternion originalRotation;

void Start()
{
    originalRotation = transform.rotation;
}


void Update() {
    float axis = Input.GetAxis("Horizontal");

    // If we're not pressing left or right, rotate back toward neutral.    
    var targetRotation = originalRotation;

    // If we are pressing left/right, our target should be somewhere to the left/right.
    if(!Mathf.Approximately(axis, 0)) {
        var twist = Quaternion.Euler(0, 0, 90 * Mathf.Sign(axis));
        targetRotation = transform.rotation * twist;
    }

    // Smoothly rotate toward the current target, at turnSpeed degrees per second.
    transform.rotation = Quaternion.RotateTowards(
                             transform.rotation, 
                             targetRotation, 
                             turnSpeed * Time.deltaTime
                         );
}

This code uses a linear rate of change, but you could also add damping/easing in and out if you like.

\$\endgroup\$
1
  • \$\begingroup\$ DMGregory you have done it again! It worked for the rotation smoothly reverting back to the original position by removing the transform.rotation.eulerAngles.x part of the code and implementing your code worked a treat! However 1 question still remains which the if rotation exceeds a certain amount for both sides die. \$\endgroup\$
    – Object
    Jun 3 '20 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.