Search Unity

slow rotation from point to point

Discussion in 'Scripting' started by RickP, Apr 7, 2010.

  1. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    Does anyone know how I would do this, or any resources out there I can learn from on how to slowly rotate a model from one point to another?

    The idea is to move a model from waypoint to waypoint and to slowly rotate the model from the current waypoint to the next waypoint. I have the movement done but LookAt() instantly rotates and it looks horrible.

    I tried below, but it's saying UnityEngine.Component.transform is read only.
     
  2. GamersHeaven

    GamersHeaven

    Joined:
    Feb 15, 2010
    Posts:
    88
    Iam using this for my turret!
    Code (csharp):
    1.  
    2. // Put the barrel on the top of the model as this look at rotation is restricted to z axis !
    3. //Remove ( , Vector3.forward ) at the end so will you get an all axis smooth look at rotation.
    4. // Also remove  these ( newRotation.x = 0.0;   newRotation.y = 0.0;  )
    5. // Get the target rotation
    6. var newRotation = Quaternion.LookRotation(transform.position - target.transform.position, Vector3.forward);
    7. newRotation.x = 0.0;
    8. newRotation.y = 0.0;
    9. // Smoothly rotate towards the target .
    10. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, speed*Time.deltaTime);
    11.  
     
  3. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    Don't use Time.time use Time.deltaTime
    That documentation really needs to be fixed
     
  4. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    Cool, I have the following and it works perfectly.

    Code (csharp):
    1.  
    2.     // rotate towards the currentWaypoint
    3.     var targetPoint = currentWaypoint.transform.position;
    4.     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
    5.     transform.localEulerAngles.x = 0;
    6.     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * currentWaypoint.rotationSpeed);
    7.  
     
  5. GamersHeaven

    GamersHeaven

    Joined:
    Feb 15, 2010
    Posts:
    88
    Always fun to help :wink: