Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

rigidbody rotatetowards

Discussion in 'Scripting' started by dudedu, Jul 23, 2020.

  1. dudedu

    dudedu

    Joined:
    Jul 14, 2020
    Posts:
    8
    hey, how do you get maxDegreesDelta?
    im trying to make a rigidbody rotateTowards a click but havent had much luck aside from using Quaternion.LookRotation(which is great but too snappy). help pls

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.  
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.             bool hitSomething = Physics.Raycast(ray, out RaycastHit hitInfo);
    8.  
    9.             if (hitSomething)
    10.             {
    11.                 Vector3 clickedWorldPoint = hitInfo.point;
    12.                 _rigidbody.AddForce((clickedWorldPoint - transform.position).normalized * moveSpeed);
    13.  
    14.                 float singleStep = rotationSpeed * Time.deltaTime;
    15.                 //Vector3 targetDirection = _rigidbody.position - clickedWorldPoint;
    16.                 //Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 1.0f);
    17.                 //transform.rotation = Quaternion.LookRotation(newDirection);
    18.  
    19.                 _rigidbody.transform.rotation = (Quaternion.RotateTowards(transform.position, clickedWorldPoint, ??));
    20.  
    21.  
    22.                 //_rigidbody.transform.rotation = (Quaternion.LookRotation(clickedWorldPoint - transform.position));
    23.  
    24.             }
    25.         }
    26.     }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,011
    With your code, if your raycast was successful then the code to rotate the object is only executed for that frame.

    As you don't want it to just snap to a rotation, calc a target rotation if your raycast is successful, store it in a variable and then lerp or rotate towards that each frame.
     
    dudedu likes this.
  3. dudedu

    dudedu

    Joined:
    Jul 14, 2020
    Posts:
    8
    wow, yea youre right. thx for pointing that out. how do you store a variable? (sry for such a noob question.. using 'clickedWorldPoint' outside of this if statement has been giving me errors lol)
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,011
    In the same way you store a reference to the rigid body component.
    Code (CSharp):
    1. public float RotationSpeed = 2f;
    2.  
    3. private Camera _mainCam;
    4. private Vector3 _hitPoint;
    5. private Quaternion _targetRot;
    6.  
    7. private void Start()
    8. {
    9.     _mainCam = Camera.main;
    10.     _targetRot = transform.rotation;
    11. }
    12.  
    13. private void Update()
    14. {
    15.     if (Input.GetMouseButtonDown(0))
    16.     {
    17.         var ray = _mainCam.ScreenPointToRay(Input.mousePosition);
    18.  
    19.         if (Physics.Raycast(ray, out RaycastHit hitInfo))
    20.         {
    21.             _hitPoint = hitInfo.point;
    22.  
    23.             _targetRot = Quaternion.LookRotation(_hitPoint - transform.position, Vector3.up);
    24.         }
    25.     }
    26.  
    27.     transform.rotation = Quaternion.Lerp(transform.rotation, _targetRot, RotationSpeed * Time.deltaTime);
    28. }
     
    dudedu likes this.
  5. dudedu

    dudedu

    Joined:
    Jul 14, 2020
    Posts:
    8
    duude. it makes so much sense now. you put the information you want at the beginning so even if it goes through the IFs and what else you can still pull it out and slap it onto a different function. aaaha, its so obvious! you da bes.
    with this new info i was able to move things around so the input is taken in by Update and the physics magic is school bussed through FixedUpdate :D YES. also the rotation is smooth as butta, tysm.
     
unityunity