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):
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;
}