Ryan
Ryan

Reputation: 1974

Unity C# CharacterController Script

I have a simple script attached to a ball model in unity. In a effort to control the ball, I have attempted to mimic this example provided by the documentation. The issue I receive is my ball visually rotates half as fast as its physical rotation changes.

Ex: The ball will rotate visually 180 degrees when you make a 360 degree physics rotation.

public class PlayerController : MonoBehaviour {

public float MoveSpeed;
public float RotationSpeed;
CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * RotationSpeed, 0));
    Vector3 forward = Input.GetAxis("Vertical") * transform.TransformDirection(transform.forward) * MoveSpeed;

    controller.Move(forward);
}

What I would like to accomplish is for the ball to rotate in alignment with the rotation of its physics controls.

Upvotes: 0

Views: 786

Answers (1)

Ian H.
Ian H.

Reputation: 3929

Just get rid of the transform.TransformDirection(transform.forward), since you are trying to transform the local forward vector of the transform to the forward vector of the same transform.

Just use a simple transform.forward instead.

Upvotes: 3

Related Questions