0
\$\begingroup\$

How could I rotate my character that I'm using a rigidbody to move. I tried using transform.eulerAngles and changing those but the camera feels very jittery.

\$\endgroup\$
  • \$\begingroup\$ Please show us your code if you want detailed help improving it. It would also be useful to show the inspector configuration of the character object and its relationship to the camera in the scene hierarchy. Don't forget to tag your question with Unity if that's the engine you're using. \$\endgroup\$ – DMGregory Jun 5 '19 at 21:33
0
\$\begingroup\$

There are different ways for rotating object's, For example:: rotation using rigidbody, rotation using Quaternion and ... Choosing the technique for rotating an object depends on the "type of object", By "Type of object" I mean whether the object has a rigidbody component, character controller or nothing attached it. Following the technique of rotation is best suited only for those object which has rigidbody component attached to it.

here is my code:

    private GameObject target;
    float rotateSpeed=0.5f ;// This Must between 0 and 1

    void Start () {

    target = GameObject.FindGameObjectWithTag("target") as GameObject;

    }

    void FixedUpdate () {

    if(target == null){
    Debug.Log("ERROR");
    return;
   }

    Vector3 targetPosition = target.transform.position; 
    Vector3 inverseVect = transform.InverseTransformPoint(targetPosition); 

    // calculate angle by which you have to rotate
    // Note -: This angle is calculated every Frame of FixedUpdate
    float rotationAngle = Mathf.Atan2(inverseVect.x,inverseVect.z) * Mathf.Rad2Deg; 

    // Now calculate  rotationVelocity to be applied every frame
    Vector3 rotationVelocity = (Vector3.up * rotationAngle) * rotateSpeed * Time.deltaTime; 

    // Calaculate his delta velocity   i.e required - current 
    Vector3 deltavel = (rotationVelocity - rigidbody.angularVelocity); 

    // Apply the force to rotate
    rigidbody.AddTorque(deltavel, ForceMode.Impulse);
}
\$\endgroup\$

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.