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 phosfiend · Aug 25, 2010 at 12:25 AM · positiontimesmoothtranslate

Change position (hover) over time with easing?

Howdy, I've got a bunch of objects i'd like to animate floating (moving up then back down their respective Y position) smoothly. The following is what I'm using to also rotate the objects slightly over time:

private var rotating1 = false;

function RotateObject1 (thisTransform : Transform, degrees : Vector3, seconds : float) { if (rotating1) return; rotating1 = true;

var startRotation = thisTransform.rotation; var endRotation = thisTransform.rotation * Quaternion.Euler(degrees); var t = 0.0; var rate = 1.0/seconds;

while (t < 1.0) { t += Time.deltaTime * rate; thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, Mathf.SmoothStep(0.0, 1.0, t)); yield; }

rotating1 = false;

}

as seen here: http://answers.unity3d.com/questions/7380/rotate-90-degrees-over-time-with-parabolic-easing

I've tried to modify the code above to serve my translation needs, but I don't really know what I'm doing. Any suggestions?

I want the amount to lift to be flexible (a variable) so I can assign it differently to different objects.

Thanks,

Richard

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 3dDude · Aug 25, 2010 at 01:10 AM 0
Share

could you just use transform.Rotate()?

avatar image phosfiend · Aug 25, 2010 at 04:58 AM 0
Share

The rotation aspect is working, splendidly. I need a function that does something similar to the above code, but for position.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by skovacs1 · Aug 25, 2010 at 04:26 PM

If you just want to hover, you can take the same code and apply it to position.

private var floating : bool = false;

function Float(objTransform : Transform, amplitude : Vector3, duration : float) { if (floating) return; floating = true;

 var startPosition : Vector3 = objTransform.position;
 var endPosition : Vector3 = objTransform.position + amplitude;
 var t : float = 0.0;
 var halfRate = 2 / duration; //we need the rate for each half

 while (t &lt; 1.0) { //Rise
     t += Time.deltaTime * halfRate;
     objTransform.position = Vector3.Lerp(startPosition, endPosition, 
                                          Mathf.SmoothStep(0.0, 1.0, t));
     yield;
 }
 while (t &gt; 0.0) { //Fall
     t -= Time.deltaTime * halfRate;
     objTransform.position = Vector3.Lerp(startPosition, endPosition,
                                          Mathf.SmoothStep(0.0, 1.0, t));
     yield;
 }

 floating = false;

}

Since this script is only able to run one at a time because of the boolean, if you are making the object it is attached to float, you don't need to pass the Transform. Also, this seems to be written to run as a co-routine so that it will finish the duration, meaning that in order to keep floating, you would have to call this after the duration every time. All of this seems sort of needless - why not try:

private var floating : bool = false; private var floatTime : float; private var floatOrigin : float; private var floatPeak : Vector3;

//These are public so you can set/tweak them, etc. var floatAmplitude : Vector3 = Vector3.up * 5; var floatFrequency : float = 0.5; var floatDuration : float = Mathf.Infinity;

function Update() { if(floating) Float(); }

function StartFloating() { //Setup floating = true; floatTime = 0.0; floatOrigin = transform.position; floatPeak = transform.position + floatAmplitude; }

function Float() { if (!floating) return;

 floatTime += Time.deltaTime;
 var t = ((floatTime * floatFrequency) % 1) * 2;
 if(t &gt;= 1) t = 2 - t; //float back down
 transform.position = Vector3.Lerp(floatOrigin, floatPeak,
                                   Mathf.SmoothStep(0.0, 1.0, t));

 if (floatTime &gt;= floatDuration) floating = false;

}

In either case, you could also use localPosition if you want it orientation relative.

The real problem is if we want to float while performing other motion. We want to be adding to position, not setting it. The problem is that if we simply add to position, it is possible that we might float too little or too far in a given direction. One solution is to only float on one axis and then only set that value using for example:

    var floatOrigin : float = transform.position.y;
    var floatPeak : float = transform.position.y + floatAmplitude;
    //and
    transform.position.y = Mathf.SmoothStep(floatOrigin, floatPeak, t);

But that just avoids the problem. What you really need to do in order to solve this is find the adjustment needed to get to the target position, which might look like so:

private var floating : bool = false; private var floatTime : float; private var floatPosition : float;

//These are public so you can set/tweak them, etc. var floatAmplitude : Vector3 = Vector3.up * 5; var floatFrequency : float = 0.5; var floatDuration : float = Mathf.Infinity;

function Update() { if(floating) Float(); }

function StartFloating() { //Setup floating = true; floatTime = 0.0; floatPosition = Vector3.zero; }

function Float() { if (!floating) return;

 floatTime += Time.deltaTime;
 var t = ((floatTime * floatFrequency) % 1) * 2;
 if(t &gt;= 1) t = 2 - t; //float back down
 var floatDesination : Vector3 = Vector3.Lerp(Vector3.zero, floatAmplitude,
                                              Mathf.SmoothStep(0.0, 1.0, t));
 var FloatAdjustment : Vector3 = floatDestination - floatPosition;
 transform.position += FloatAdjustment;
 floatPosition = floatDestination;

 if (floatTime &gt;= floatDuration) floating = false;

}

or with the other code:

private var floating : bool = false;

function Float(objTransform : Transform, amplitude : Vector3, duration : float) { if (floating) return; floating = true;

 var currentPosition : Vector3 = Vector3.zero;
 var targetPosition : Vector3;
 var adjustment : Vector3;
 var t : float = 0.0;
 var halfRate = 2 / duration; //we need the rate for each half

 while (t &lt; 1.0) { //Rise
     t += Time.deltaTime * halfRate;
     targetPosition = Vector3.Lerp(Vector3.zero, amplitude, 
                                          Mathf.SmoothStep(0.0, 1.0, t));
     adjustment = targetPosition - currentPosition;
     objTransform.position += FloatAdjustment;
     currentPosition = targerPosition;
     yield;
 }
 while (t &gt; 0.0) { //Fall
     t -= Time.deltaTime * halfRate;
     targetPosition = Vector3.Lerp(Vector3.zero, amplitude, 
                                          Mathf.SmoothStep(0.0, 1.0, t));
     adjustment = targetPosition - currentPosition;
     objTransform.position += FloatAdjustment;
     currentPosition = targerPosition;
     yield;
 }

 floating = false;

}

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

No one has followed this question yet.

Related Questions

Checking position doesn't work? 2 Answers

Make something arrive at position in exactly X seconds? 2 Answers

how to change a value over time in a coroutine ? 2 Answers

Translating a rigidbody smoothly 1 Answer

Changing GUITexture's position through code (relative to screen) 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