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 Gamexcb · Mar 20, 2012 at 03:22 PM · rotationvector3quaternionlerp

Issues with Lerps

I want an object to be able to move between a few position and have a different rotation at each position. This is the script I am using:

var duration : float = 1.0; private var startPoint : Vector3; private var startTime : float; var move : boolean = false; function Start() { startTime = Time.time; } function Update () { if(move == true){ transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration); transform.rotation = Quaternion.Lerp(transform, transform, (Time.time - startTime) / duration); } if (move == false){ transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration); transform.rotation = Quaternion.Lerp(transform, transform, (Time.time - startTime) / duration); } }

The Issue is that when the move variable is toggled. The object appears directly at the specified transform without the motion in between. Shouldn't it smoothly transition to the specified transform? Any help would be very appreciated.

Comment
Add comment · Show 2
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 Kryptos · Mar 20, 2012 at 04:59 PM 0
Share

Please format your script correctly. And fix it, I'm pretty sure this does not compile:

 transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration);

Because transform is a Transform object not a Vector3.

avatar image p.hufnagl · Feb 20, 2014 at 09:54 AM 0
Share

Can someone rate this post down plz

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by meat5000 · Aug 05, 2013 at 07:24 PM

I know this is old but I read it so I figure someone else might.

transform.position = Vector3.Lerp works for me. I notice in your script transform.position = Vector3.Lerp(transform,transform etc where the 'from' and 'to' are both the same place, so no movement (so I assume you are snapping in objects in editor?)

Anyway, your issue: Lerp and Slerp can be treated like weighting in that its third field (where t usually goes) is between 0 and 1 only. As your duration is 1 and startTime is only called once in start() function it seems that t becomes greater than 1 in 1 second and hence your lerp will always Snap To the target (which is its starting position in your case?).

You need to normalise your t factor so that over a chosen duration of time your varibles scale up to 1. For this I like to use my own timers:

lerpTimer += Time.deltaTime; as for me its easier to get my head around. Then when the lerp is complete, reset your timer; lerpTimer = 0;

I had troubles and I read that I was using lerp incorrectly to begin. It does NOT perform a full smooth movement when called but RETURNS the SCALED DIFFERENCE between two factors every update/fixedupdate with which you are responsible for maintaining and changing the t factor to manipulate the transition yourself. Imagine t as a slider and you are responsible for moving the slider in your code, or as it is, providing a change between 0 and 1, and this decides the speed and smoothness of your transition.

extract of my game :

transform.position = Vector3.Slerp(cannonMoveFrom, cannonMoveTo, lerpTimer/spawnLerp); lerpTimer += Time.fixedDeltaTime;

here, spawnLerp is fixed value, say 10. So, when lerpTimer hits 10 seconds t = 1; in 5 seconds, 5/10 = 0.5 so lerp is halfway along and transform.position is instantaneously set to halfway between cannonMoveFrom and cannonMoveTo.

I was stumped for a while as I was calling lerp from within a timed if() function and of course the lerp calcaulation was only being performed once every 3 seconds, or whatever. I needed the lerp to happen seconds after an event so I used the if() to reset the timers and used the else() for the actual Lerp. Worked like a charm.

Hope this helps at least 1 person, with which I will be happy.

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

Answer by Owen-Reynolds · Mar 20, 2012 at 11:27 PM

Lerp is mostly useful for giving a "fast then slow" zooming camera move. For regular motion, easier to use:

 transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed);

Then all you have to do is set target to where you want to go. You don't have to worry about startTime anymore. Instead of setting duration for how long it takes, adjust moveSpeed. You can still use move (T/F), but don't have to -- setting target to where you are now will automatically stop you.

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

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

7 People are following this question.

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

Related Questions

How to smoothly rotate to certain directions using input axis 1 Answer

How to know if a gameobject is rotating? 1 Answer

How would I smooth out a Quaternion? 2 Answers

LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers

Instantiate GameObject towards player 0 Answers


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