Search Unity

Rotating to desired transform.forward with Physics

Discussion in 'Scripting' started by Victorer, Nov 26, 2015.

  1. Victorer

    Victorer

    Joined:
    Nov 26, 2015
    Posts:
    2
    This is driving me nuts. I have this code:

    Code (csharp):
    1. void FixedUpdate () {
    2.    Vector3 desiredForward = Camera.main.transform.position-transform.position;
    3.    Quaternion desiredRotation = Quaternion.LookRotation(desiredForward);
    4.    float amount = Quaternion.Angle(transform.rotation, desiredRotation) / 180 * Time.fixedDeltaTime;
    5.    GetComponent<Rigidbody>().AddTorque(desiredRotation * Vector3.up * amount, ForceMode.Acceleration);
    6.    GetComponent<Rigidbody>().AddTorque(desiredRotation * Vector3.right * amount, ForceMode.Acceleration);
    7.    GetComponent<Rigidbody>().AddTorque(desiredRotation * Vector3.forward * amount, ForceMode.Acceleration);
    8. }
    All I want to do is for the GameObject to rotate and look at the main camera, via Physics using AddTorque() on its Rigidbody.

    I know how to do this by modifying the transform directly but I want to do it via Physics. I know it's possible but I just can't figure out how to do it! I crave this knowledge.

    If I do this instead of the last four lines...

    Code (csharp):
    1. transform.rotation = desiredRotation;
    ...then the GameObject will look right at the main camera, so I know the quaternion is correct. But how do I manipulate the Rigidbody to gradually rotate to the desiredRotation over several fixed updates?

    + The first behaviour I desire is for the GameObject to rotate to the desired rotation and then go back and forth (always "passing through" the transform.forward that makes it look straight at the main camera).
    + The second behaviour is that it should smoothly slows down its rotation --using only AddTorque()-- and then finally stops at the desired rotation.
    + But first things first, having it swing back and forth at the desired rotation feels like the key to solving this problem.

    Remember that I don't want to "cheat" by modifying the angular velocity or transform directly. I don't want to use rigidbody.MovePosition() or rigidbody.MoveRotation() or anything like that. I just want to see how to rotate a GameObject with physics only, for learning purposes.

    I think the first step is to figure out the direction it wants to rotate on the "rotational sphere". Multiplying desiredRotation with for example Vector3.up is probably fine (?) but I need to also multiply it with -1 if the desired rotation is downwards, but I can't figure out a good way to do it.

    Like I said, this is driving me nuts. I really hope someone can help me!
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its not really wrong to use torque to rotate. You just need to get the math right. You've got a bunch of weird things happening here.
    • Torque is a force. There is no need to use Time.deltaTime anywhere.
    • AddRelitiveTorque is normally a better method, adding torque in world space is normally not all that useful
     
  4. Victorer

    Victorer

    Joined:
    Nov 26, 2015
    Posts:
    2
    Whether it's correct or not is irrelevant to me, I want to know how to do it with AddTorque() (or AddRelitiveTorque()) simply because I crave the knowledge. I know it's possible and I'm slowly becoming insane by the fact that I can't figure out how. I also disagree that it would be "doing it wrong" simply by rotating using Physics, it's just doing it different.

    Regarding your link, is the code in the answer by "Chybis" what I should be looking at? (Not the green one marked as "best answer"?)


    Yeah I'm not very good at math, which is probably why I can't figure this out in the first place. I was just using deltaTime to scale down the amount of force applied every fixed update.
     
  5. jarratt51

    jarratt51

    Joined:
    Mar 24, 2019
    Posts:
    1