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): #pragma strict var maxVelocityChange = 10.0; var canJump = true; var jumpHeight = 2.0; var jumpinterval : float = 1.5; private var nextjump : float = 1.2; public var speed : float = 4; public var chest : Transform; public var target : Transform; private var moveAmount : float; public var gravity : float = 25; public var rotateSpeed : float = 8.0; public var dampTime : float = 3; public var mask : LayerMask; public var downcastrange : float =1.2; private var horizontalSpeed : float; public var addvector : Vector3; var grounded : boolean; private var running : boolean = false; private var originalSpeed : float; var isaiming : boolean = false; private var forward : Vector3 = Vector3.forward; private var moveDirection : Vector3 = Vector3.zero; private var right : Vector3; private var canjump : boolean = false; private var isjumping : boolean = false; private var chestoriginal : Quaternion; function Awake() { rigidbody.freezeRotation = true; rigidbody.useGravity = false; } function Start() { originalSpeed = speed; chestoriginal = chest.transform.rotation; } function FixedUpdate() { var animator = GetComponent(Animator); forward = camera.main.transform.forward; right = new Vector3(forward.z, 0, -forward.x); var hor = Input.GetAxis("Horizontal"); var ver = Input.GetAxis("Vertical"); var targetDirection : Vector3 = (hor * right) + (ver * forward); var animver = Mathf.Round(ver); var animhor = Mathf.Round(hor); if (isaiming) { var relativePos = target.transform.position - chest.transform.position; var lookrotation = Quaternion.LookRotation(relativePos,Vector3.up); lookrotation.x = 0; lookrotation.z = 0; transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation,Time.deltaTime * rotateSpeed); } else { if (targetDirection != Vector3.zero) { var lookrotation2 = Quaternion.LookRotation(targetDirection,Vector3.up); lookrotation2.x = 0; lookrotation2.z = 0; transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation2,Time.deltaTime * rotateSpeed); } } animator.SetFloat("horizontal",animhor,dampTime,0.2); animator.SetFloat("vertical",animver,dampTime,0.2); if(grounded) { // Calculate how fast we should be moving var targetVelocity = targetDirection; targetVelocity *= speed; // Apply a force that attempts to reach our target velocity var velocity = rigidbody.velocity; var velocityChange = (targetVelocity - velocity); velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange); velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange); velocityChange.y = 0; rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // Jump if (Input.GetButton("Jump") && Time.time > nextjump) { nextjump = Time.time + jumpinterval; rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z); animator.SetBool("jump",true); } else { animator.SetBool("jump",false); } } animator.SetBool("grounded",grounded); // We apply gravity manually for more tuning control rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0)); testground(); } function testground() { yield WaitForSeconds(0.3f); var hit : RaycastHit; if(Physics.Raycast(transform.position + Vector3.up, Vector3.down, hit, downcastrange , mask )) { grounded = true; } else { grounded = false; } } function LateUpdate() { if (isaiming) { var relativePos = target.transform.position - chest.transform.position; var lookrotation = Quaternion.LookRotation(relativePos,Vector3(0,-1,0)); var addEuler = Quaternion.Euler(addvector); var wantedrotation = lookrotation * addEuler; chest.transform.rotation = Quaternion.Lerp(chest.transform.rotation,wantedrotation,Time.time * 0.3); } } function CalculateJumpVerticalSpeed () { // From the jump height and gravity we deduce the upwards speed // for the character to reach at the apex. return Mathf.Sqrt(2 * jumpHeight * gravity); }
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?
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
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.