Home > Software engineering >  Issue with rotation in Unity?
Issue with rotation in Unity?

Time:03-15

I'm trying to Rotate my GameObject using the script around Y axis:

    rotateDir = Input.GetAxis("Horizontal");
    rb.AddRelativeTorque(Vector3.up * rotateDir * rotateForce);

But my GameObject starting tilt (last picture):

enter image description here enter image description here

I have changed colliders, checked my code (I can't see any errors)< but still it doesn't work properly. Could anybody help me? thanks

CodePudding user response:

private void Update()
    {
        RotateTowardsYDirection();
    }

    public void RotateTowardsYDirection()
    {
        ////transform object to rotate with some rotation factor value
        objectToRotate.Rotate(0, Input.GetAxis("Horizontal") * yRotationFactor * Time.deltaTime, 0);
    }

CodePudding user response:

There are many ways to rotate gameObject in unity. But the best way to rotate is using EularAngles

// Transform.eulerAngles represents rotation in world space
void Update() {
  currentEulerAngles  = new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;

  //apply the change to the gameObject
  transform.eulerAngles = currentEulerAngles;
}
  • Related