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 keefus · Jan 19, 2011 at 04:25 PM · transformpositionlerp

lerp not working

Hi,

I'm trying to do a simple lerp between positions and I cant get it working, I'm assuming I need to put the lerp command in a particular function? but I'm struggling to get it to work with my button presses.

var shelfpos : Vector3; shelfpos = transform.position; var otherObject : GameObject; otherObject = GameObject.Find("First Person Controller/Main Camera/held_item"); private var startTime : float; var duration : float = 1.0; var inhand = false; var origrot;

function Start () { var shelfpos : Vector3; origrot = transform.rotation; }

function Update () {

var somePos : Vector3 = otherObject.transform.position; //get position of helditem object

// if object is selected move to helditem object

if (inhand == true) { transform.position = Vector3.Lerp (shelfpos, somePos,Time.time); }

if (inhand == false) { transform.position = Vector3.Lerp (transform.position, shelfpos,Time.time); transform.rotation = origrot; }

}

function OnMouseUp () { inhand = true; }

function OnGUI () {

if (inhand == true) if (GUI.Button (Rect (170,250,150,30),"Return To Shelf")) inhand = false;

}

Any help would be cool.

Comment
Add comment · Show 3
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 DaveA · Jan 19, 2011 at 04:33 PM 0
Share

inhand is declared outside these functions, so it's available to all these functions, yes?

avatar image keefus · Jan 19, 2011 at 04:36 PM 0
Share

yes, I suppose it only needs to be inside the update function though. it simply tells the object which position it needs to be in.

avatar image Eric5h5 · Jan 19, 2011 at 07:05 PM 0
Share

See here for how to use lerp: http://answers.unity3d.com/questions/6949/can-someone-explain-how-using-time-deltatime-as-t-in-a-lerp-actually-works/6950#6950

2 Replies

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

Answer by Jessy · Jan 19, 2011 at 05:09 PM

The key to your problem is that after the first second of your game, there can no longer be a lerp, because of your use of Time.time as the lerp value. To start off with, try resetting the lerp value to zero every time you change inhand. Also, name it inHand. And name origrot origRot while you're at it (if you really feel the need to shorten the name so much; originalRotation should be fine on today's computer monitors.). I thought it was some kind of demon name the first time I saw it.

Comment
Add comment · 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
0
Best Answer

Answer by keefus · Jan 24, 2011 at 11:34 AM

Thanks for all the help.

I managed to get it working using the simplest solution I could find, which was using Time.deltaTime instead. I also added a Quaternion.Slerp to smooth out the rotation. I have enclosed the updated code in case somebody might find it useful. I have updated the variable names also, just out of interest why is it better to use inHand instead of inhand? anyway, heres the code...

var shelfpos : Vector3; var otherObject : GameObject; var inHand = false; var originalRotation; shelfpos = transform.position; otherObject = GameObject.Find("First Person Controller/Main Camera/held_item");

function Start () { var shelfpos : Vector3; originalRotation = transform.rotation; }

function Update () { var speed = 6; var somePos : Vector3 = otherObject.transform.position; //get position of helditem object

// if object is selected move to helditem object

 if (inHand == true)
 {
         transform.position = Vector3.Lerp (transform.position, somePos,Time.deltaTime*speed);
 }



 if (inHand == false)
 {
         transform.position = Vector3.Lerp (transform.position, shelfpos,Time.deltaTime*speed);  // move back to shelfpos

     //rotate back to originalRotation
     transform.rotation =
     Quaternion.Slerp (transform.rotation, originalRotation, Time.deltaTime*speed);

 }

}

function OnMouseUp () { inHand = true; }

function OnGUI () {

if (inHand == true) if (GUI.Button (Rect (170,250,150,30),"Return To Shelf")) inHand = false;
}

Kind Regards

Comment
Add comment · Show 2 · 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 Jessy · Jan 24, 2011 at 02:13 PM 0
Share

"inhand" is not one word, but two. camelCase is a really helpful convention for reading multi-word variables, and can be found throughout the Unity API.

avatar image keefus · Jan 24, 2011 at 02:20 PM 0
Share

That explains it, I can see how that is a better na$$anonymous$$g convention. Thanks for all the help.

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

No one has followed this question yet.

Related Questions

Turning character slowly with Slerp 1 Answer

move a gameobject to left/right while going up 1 Answer

Move object while rotating another one 1 Answer

Moving Maincamera slowly to another position 1 Answer

Lerp not working 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