- Home /
 
What is interfering with my Lerp?
Just can't figure this out. I have an object with the following script attached that can be dragged along the Y axis with a mouse/finger, and when it collides with a trigger, it should lerp to another position. I can get the lerp to work without the dragging script, and the dragging script to work without the lerp, but once I put them together, no lerping happens and the object just snaps to 0,0,0. What about my code is interfering with the lerp?
 // DRAGGING VARIABLES
 
     var moveLimit = .5;
     var collisionMoveFactor = .01;
     var freezeRotationOnDrag = true;
     var cam : Camera;
     private var myRigidbody : Rigidbody;
     private var myTransform : Transform;
     private var canMove = false;
     private var yPos : float;
     private var freezeRotationSetting : boolean;
     private var sqrMoveLimit : float;
     private var camTransform : Transform;
 
 // LERPING VARIABLES
 
     var isLerping = false;
     var lerpTimer = 0.0;
     var lerpSpeed = 0.1;    
     var Begin_Rotation = transform.eulerAngles;
     var End_Rotation = Vector3(-90,0,0);
     var Begin_Position = transform.position;
     var End_Position = Vector3(-4.229075,3,2.676687);
     
     
     function Start () {
         myRigidbody = rigidbody;                                    
         myTransform = transform;                                      
         if (!cam) {                                                   
             cam = Camera.main;                                         
         }
         camTransform = cam.transform;                                
         sqrMoveLimit = moveLimit * moveLimit;                       
     }
     
     
     function OnMouseDown () {
         canMove = true; 
         freezeRotationSetting = myRigidbody.freezeRotation;
         myRigidbody.freezeRotation = freezeRotationOnDrag;
         yPos = myTransform.position.y;
     }
     
     function OnMouseUp () {
         canMove = false;    
     }
     
     function OnTriggerEnter (other : Collider)
     {                            
         if (tag == other.gameObject.tag){
             isLerping = true;                        
             }
     }
     
     function FixedUpdate () {
       
         if (!canMove) return;
         myRigidbody.velocity = Vector3.zero;
         myRigidbody.angularVelocity = Vector3.zero;
         myTransform.position.y = yPos;
         var mousePos = Input.mousePosition;
         var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
         myRigidbody.MovePosition(myRigidbody.position + move);
       
     // LERPING CODE    
             
         if (isLerping == true){
             lerpTimer += Time.deltaTime;
             transform.eulerAngles = Vector3.Lerp (Begin_Rotation, End_Rotation, lerpTimer*lerpSpeed);
             transform.position = Vector3.Lerp(Begin_Position, End_Position, lerpTimer*lerpSpeed); 
             }
         }
 
 
              try this
 function FixedUpdate () {
  
 if (can$$anonymous$$ove){// return;
      myRigidbody.velocity = Vector3.zero;
      myRigidbody.angularVelocity = Vector3.zero;
      myTransform.position.y = yPos;
      var mousePos = Input.mousePosition;
      var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
      myRigidbody.$$anonymous$$ovePosition(myRigidbody.position + move);
 }
 // LERPING CODE
  
 if (isLerping){
 
 var rotate = Quaternion.Euler( End_Rotation );
 //myRigidbody.$$anonymous$$oveRotation(myRigidbody.rotation * rotate);
 myTransform.rotation = Quaternion.Lerp(myTransform.rotation, rotate, Time.time*lerpSpeed );
 //myRigidbody.$$anonymous$$ovePosition(myRigidbody.position + End_Position);
 myTransform.position = Vector3.Lerp( myTransform.position, End_Position, Time.time*lerpSpeed );
 }
 
                  }
Thanks, Sundar. I added your changes, but it still behaves the same... the shape still ends up at 0,0,0 with its default rotation. So confused by this.
$$anonymous$$ade some changes to the above code, test and see whether this solves your lerp / move issue
Thanks again. I get an error on line 16 saying the best overload for the method 'UnityEngine.Quaternion, UnitEngine.Quaternion, float) is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)'.
Also the variable "move" is declared and used twice, but I made adjustments for that.
But even when I comment out lines 16 and 17 (to ignore the rotation), now nothing happens when the shape is dragged across the trigger.
Answer by Sundar · Dec 16, 2013 at 07:40 PM
most likely you have not given any values for End_Rotation and End_Position at inspector. Move both values to Start()
  function Start () {
 myRigidbody = rigidbody;
 myTransform = transform;
 if (!cam) {
 cam = Camera.main;
 }
 camTransform = cam.transform;
 sqrMoveLimit = moveLimit * moveLimit;
 End_Rotation = Vector3(-90,0,0);
 End_Position = Vector3(-4.229075,3,2.676687);
 }
 
              Brilliant. That was it. Can't thank you enough, Sundar!
I'm realizing now that the smooth lerp is not happening in iOS. The shape is making the journey to it's new destination, but it is happening instantaneously. Any ideas on where to start?
This is just a suggestion, you divide lerpSpeed/100.0f and test, if it is too slow change its value until you get your desired speed.
Your answer
 
             Follow this Question
Related Questions
Slerp, lerp or something else? 3 Answers
My Lerp() is not working 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Help with .Lerp 2 Answers
Moving Platform Moves instantly. 1 Answer