Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RussellFincher · Dec 13, 2013 at 06:09 PM · javascriptlerp

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); 
             }
         }
 
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sundar · Dec 13, 2013 at 11:15 PM 0
Share

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 );
 }

}

avatar image RussellFincher · Dec 16, 2013 at 04:41 PM 0
Share

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.

avatar image Sundar · Dec 16, 2013 at 05:22 PM 0
Share

$$anonymous$$ade some changes to the above code, test and see whether this solves your lerp / move issue

avatar image RussellFincher · Dec 16, 2013 at 05:41 PM 0
Share

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.

avatar image Sundar · Dec 16, 2013 at 05:58 PM 0
Share

Lets do step by step, Check whether this works.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RussellFincher · Dec 16, 2013 at 07:59 PM 0
Share

Brilliant. That was it. Can't thank you enough, Sundar!

avatar image RussellFincher · Dec 18, 2013 at 07:52 PM 0
Share

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?

avatar image Sundar · Dec 18, 2013 at 07:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges