Search Unity

Smooth rotation broke my head.

Discussion in 'Scripting' started by nbg_yalta, Apr 4, 2014.

  1. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    I've tried almos all the ways to make my rotaion smooth with no luck;
    I have a capsule collider with rigidbody. I want to make a simle fish behavior with jump from the water and fall back.

    Jump:

    Code (csharp):
    1. if(Time.time > JumpDelay)
    2.         {
    3.             JumpDelay = Time.time + JumpRate;
    4.             rigidbody.AddForce(Vector3.up*jumpForce, ForceMode.Impulse);
    5.         }
    rotate to move dir

    Code (csharp):
    1. transform.up = rigidbody.velocity;
    How to smooth?
     
  2. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    You can use this to set the rotation based on the velocity
    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    3.  
    To rotate smoothly, you can use Quaternion.RotateTowards like so
    Code (csharp):
    1.  
    2. Quaternion toRotation = Quaternion.LookRotation(rigidbody.velocity);
    3. transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, Time.deltaTime * yourSmoothingValue);
    4.  
     
  3. mansim

    mansim

    Joined:
    Apr 3, 2014
    Posts:
    6
  4. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    I've tried this way, and it won't rotate properly.. The camera is looking from the side with positive Z -> right. I need to rotate my fish in negative, or positive X axis.