Search Unity

rotation is not smooth

Discussion in 'Scripting' started by Mister-D, Dec 19, 2014.

  1. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    ive got this rigid controller script, i need to do chest aiming rotation in lateupdate to override the animations, problem is it "snaps" to the rotation instead of being smooth. i tried using time.deltatime but then nothing happens at all. can someone give me advice to get the smooth rotation?

    heres the code


    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. var maxVelocityChange = 10.0;
    5. var canJump = true;
    6. var jumpHeight = 2.0;
    7. var jumpinterval : float = 1.5;
    8. private var nextjump : float = 1.2;
    9. public var speed : float = 4;
    10. public var chest : Transform;
    11. public var target : Transform;
    12. private var moveAmount : float;
    13.  
    14. public var gravity : float = 25;
    15. public var rotateSpeed : float = 8.0;
    16. public var dampTime : float = 3;
    17. public var mask : LayerMask;
    18. public var downcastrange : float =1.2;
    19. private var horizontalSpeed : float;
    20. public var addvector : Vector3;
    21. var grounded : boolean;
    22.  
    23.  
    24.  
    25.  
    26. private var running : boolean = false;
    27. private var originalSpeed : float;
    28.  
    29. var isaiming : boolean = false;
    30.  
    31. private var forward : Vector3 = Vector3.forward;
    32. private var moveDirection : Vector3 = Vector3.zero;
    33. private var right : Vector3;
    34.  
    35. private var canjump : boolean = false;
    36. private var isjumping : boolean = false;
    37. private var chestoriginal : Quaternion;
    38.  
    39.  
    40. function Awake()
    41. {
    42.      rigidbody.freezeRotation = true;
    43.     rigidbody.useGravity = false;
    44.    
    45. }
    46. function Start()
    47. {
    48.      originalSpeed = speed;
    49.      chestoriginal = chest.transform.rotation;
    50. }
    51. function FixedUpdate()
    52. {
    53.      
    54.       var animator = GetComponent(Animator);
    55.       forward = camera.main.transform.forward;
    56.      right = new Vector3(forward.z, 0, -forward.x);
    57.      var hor = Input.GetAxis("Horizontal");
    58.      var ver = Input.GetAxis("Vertical");
    59.      var targetDirection : Vector3 = (hor * right) + (ver * forward);
    60.    
    61.    
    62.    
    63.      var animver = Mathf.Round(ver);
    64.      var animhor = Mathf.Round(hor);
    65.     if (isaiming)
    66.     {
    67.         var relativePos = target.transform.position - chest.transform.position;
    68.           var lookrotation = Quaternion.LookRotation(relativePos,Vector3.up);
    69.           lookrotation.x = 0;
    70.           lookrotation.z = 0;
    71.          transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation,Time.deltaTime * rotateSpeed);
    72.     }
    73.     else
    74.     {
    75.         if (targetDirection != Vector3.zero)
    76.         {
    77.             var lookrotation2 = Quaternion.LookRotation(targetDirection,Vector3.up);
    78.             lookrotation2.x = 0;
    79.               lookrotation2.z = 0;
    80.             transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation2,Time.deltaTime * rotateSpeed);
    81.         }
    82.     }
    83.    
    84.    
    85.    
    86.      animator.SetFloat("horizontal",animhor,dampTime,0.2);
    87.      animator.SetFloat("vertical",animver,dampTime,0.2);
    88.    
    89.    
    90.      if(grounded)
    91.      {
    92.          // Calculate how fast we should be moving
    93.         var targetVelocity = targetDirection;
    94.        
    95.         targetVelocity *= speed;
    96.         // Apply a force that attempts to reach our target velocity
    97.         var velocity = rigidbody.velocity;
    98.         var velocityChange = (targetVelocity - velocity);
    99.         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    100.         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    101.         velocityChange.y = 0;
    102.         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    103.         // Jump
    104.         if (Input.GetButton("Jump") && Time.time > nextjump)
    105.         {
    106.             nextjump = Time.time + jumpinterval;
    107.             rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    108.             animator.SetBool("jump",true);
    109.         }
    110.         else
    111.          {
    112.              animator.SetBool("jump",false);
    113.            
    114.          }  
    115.     }
    116.    
    117.      
    118.      animator.SetBool("grounded",grounded);    
    119.    
    120.    
    121.      // We apply gravity manually for more tuning control
    122.      rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    123.      testground();
    124. }
    125. function testground()
    126. {
    127.            
    128.       yield WaitForSeconds(0.3f);
    129.       var hit : RaycastHit;
    130.      
    131.       if(Physics.Raycast(transform.position + Vector3.up, Vector3.down, hit, downcastrange , mask ))
    132.       {
    133.            grounded = true;
    134.       }    
    135.       else
    136.       {
    137.            grounded = false;
    138.       }
    139.          
    140.          
    141. }
    142.  
    143. function LateUpdate()
    144. {
    145.      if (isaiming)
    146.      {
    147.          var relativePos = target.transform.position - chest.transform.position;
    148.          var lookrotation = Quaternion.LookRotation(relativePos,Vector3(0,-1,0));
    149.          var addEuler = Quaternion.Euler(addvector);
    150.          var wantedrotation =  lookrotation * addEuler;        
    151.          chest.transform.rotation = Quaternion.Lerp(chest.transform.rotation,wantedrotation,Time.time * 0.3);
    152.      }    
    153. }
    154.  
    155. function CalculateJumpVerticalSpeed ()
    156. {
    157.     // From the jump height and gravity we deduce the upwards speed
    158.     // for the character to reach at the apex.
    159.     return Mathf.Sqrt(2 * jumpHeight * gravity);
    160. }
    161.  
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Looks like you're using 'Time.time' - that's the time since the beginning of the game so it'll be a pretty huge value. That's probably why it's moving straight away. In LateUpdate you also seem to be using 'addVector' which I can't see being set up anywhere - is that declared in the object instance?
     
  3. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    addvector is set in the editor cause my chest bone isnt facing forward in z axis.
    what should i use instead of Time.time? if i use deltatime the chest doesnt rotate at all
     
  4. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    Is the chest parented ? 3d game right? if so try unparent and move transform.
     
  5. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    cant unparent, its a skinned mesh , its not a good idea to move the bones
     
  6. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    ive found a workaround and it works fine now, ive parented a empty gameobject to the "hips" bone and made that object look smooth at a target with time.deltatime and then set the chests' transform to be the same rotation as the empty gameobject in lateupdate.