Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Mister Jason · Apr 22, 2011 at 06:12 AM · objectfloating

Object floating

I've got a simple question about making an object float up and down.
I have UI above my character, and I'm trying to get it to float up and down to give a better sense of movement. Any ideas?
I've searched for similar solutions, but haven't gotten anything working. :(

Thanks in advance!

Comment
Add comment
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

7 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by aldonaletto · May 29, 2011 at 10:12 AM

If you just want a smooth floating movement, use the Mathf.Sin function:

 // Save the y position prior to start floating (maybe in the Start function):
 
   var y0:float = transform.position.y;
 
 // Put the floating movement in the Update function:
 
   transform.position.y = y0+amplitude*Mathf.Sin(speed*Time.time);

Since Sin ranges from -1 to +1, you'll have the object floating from -amplitude to +amplitude around the y0 coordinate, and the time to complete one cycle is 2 PI speed - about 6.3 seconds if speed is 1.

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 joshu_ita · Nov 15, 2013 at 07:12 PM 1
Share

Aldo, you probably meant:

 transform.position.y = y0+amplitude*$$anonymous$$athf.Sin(speed*time.time);

Otherwise you would have an error converting a float into a Vector3. By the way thanks for the hint!

avatar image aldonaletto · Nov 16, 2013 at 03:07 AM 0
Share

Oops! Thanks for the correction - answer fixed now.

avatar image karthik_laughingdots · Oct 01, 2015 at 09:49 AM 0
Share

Thank you .

avatar image
3

Answer by Mister Jason · Apr 22, 2011 at 09:22 AM

I figure out!! :)

 var floatup;
 function Start(){
     floatup = false;
 }
 function Update(){
     if(floatup)
         floatingup();
     else if(!floatup)
         floatingdown();
 }
 function floatingup(){
     transform.position.y += 0.3 * Time.deltaTime;
     yield WaitForSeconds(1);
     floatup = false;
 }
 function floatingdown(){
     transform.position.y -= 0.3 * Time.deltaTime;;
     yield WaitForSeconds(1);
     floatup = true;
 }

hope it will help.

Comment
Add comment · Show 5 · 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 POLYGAMe · May 29, 2011 at 07:51 AM 0
Share

How accurate is time within Unity? I'd like to do something similar but based upon XY coords...

avatar image POLYGAMe · May 29, 2011 at 07:56 AM 0
Share

For example (pseudo code): If object > XY1, move down... if object < XY2, move up. I'd like to have an object repeatedly moving up and down... but not bouncing, no physics necessary.

Any ideas?

avatar image SirGive · May 29, 2011 at 07:57 AM 0
Share

ask your own question and i'll help you.

avatar image POLYGAMe · May 29, 2011 at 09:09 AM 0
Share

Sorry... I thought it would be better to bring up older posts rather than creating new ones that have already been discussed. Consider me learned ;-)

avatar image SirGive · May 29, 2011 at 09:28 AM 1
Share

Next time just post your own question and provide a link to one that might be helpful to describe your problem.

avatar image
1

Answer by Meltdown · May 29, 2011 at 08:11 AM

You can also do something like this easily, and more advanced movements using Unity's built-in animation editor.

Take a look at these videos...

http://lesterbanks.com/2010/05/unity-3d-animation-tutorials/

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 POLYGAMe · May 29, 2011 at 09:15 AM 0
Share

Wow, thanks, $$anonymous$$eltdown! I didn't even know Unity had an inbuilt animation editor! This will save me tonnes of scripting work with my attack waves! Cheers!

avatar image Meltdown · May 29, 2011 at 09:43 AM 0
Share

Haha, no problem. Each day I learn something new about Unity that surprises me too :p

avatar image
1

Answer by tkamruzzaman · Nov 11, 2015 at 01:23 PM

In C# you can write something like this:

     public float amplitude;          //Set in Inspector 
     public float speed;                  //Set in Inspector 
     private float tempVal;
     private Vector3 tempPos;
     
     void Start () 
     {
         tempVal = transform.position.y;
     }
 
     void Update () 
     {        
         tempPos.y = tempVal + amplitude * Mathf.Sin(speed * Time.time);
         transform.position = tempPos;
     }
Comment
Add comment · Show 1 · 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 LietuvisKalba · Jul 15, 2018 at 09:12 PM 1
Share

The code is good. I checked it. However on line 6 you want to insert/add this bit of code tempPos = transform.position;

This is needed if you have more then 1 floating object. In my test I saw that it mushed all the coins at (0,0,0) due to the fact that we never specified where they should go after we applied the floating up and down part.

avatar image
1

Answer by Lad-Ty · Nov 27, 2017 at 05:44 PM

Since I was looking for a quick 'floating' script to save me some time and stumbled onto this topic on Google, but I still didn't get exactly what I wanted (some 'universal solution'), here is what I wrote afterwards to help other googling guys. Also includes 'floating' functionality for rotation and scale apart from position, all are togglable on/off:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SimpleFloating : MonoBehaviour {
 
     public bool animPos = true;
     public Vector3 posAmplitude = Vector3.one;
     public Vector3 posSpeed = Vector3.one;
 
     public bool animRot = true;
     public Vector3 rotAmplitude = Vector3.one*20;
     public Vector3 rotSpeed = Vector3.one;
 
     public bool animScale = false;
     public Vector3 scaleAmplitude = Vector3.one*0.1f;
     public Vector3 scaleSpeed = Vector3.one;
 
     private Vector3 origPos;
     private Vector3 origRot;
     private Vector3 origScale;
 
     private float startAnimOffset = 0;
 
 
     /**
      * Awake
      */
     void Awake() {
         origPos = transform.position;
         origRot = transform.eulerAngles;
         origScale = transform.localScale;
         startAnimOffset = Random.Range(0f, 540f);        // so that the xyz anims are already offset from each other since the start
     }
     
     /**
      * Update
      */
     void Update() {
         /* position */
         if(animPos) {
             Vector3 pos;
             pos.x = origPos.x + posAmplitude.x*Mathf.Sin(posSpeed.x*Time.time + startAnimOffset);
             pos.y = origPos.y + posAmplitude.y*Mathf.Sin(posSpeed.y*Time.time + startAnimOffset);
             pos.z = origPos.z + posAmplitude.z*Mathf.Sin(posSpeed.z*Time.time + startAnimOffset);
             transform.position = pos;
         }
 
         /* rotation */
         if(animRot) {
             Vector3 rot;
             rot.x = origRot.x + rotAmplitude.x*Mathf.Sin(rotSpeed.x*Time.time + startAnimOffset);
             rot.y = origRot.y + rotAmplitude.y*Mathf.Sin(rotSpeed.y*Time.time + startAnimOffset);
             rot.z = origRot.z + rotAmplitude.z*Mathf.Sin(rotSpeed.z*Time.time + startAnimOffset);
             transform.eulerAngles = rot;
         }
 
         /* scale */
         if(animScale) {
             Vector3 scale;
             scale.x = origScale.x * (1+scaleAmplitude.x*Mathf.Sin(scaleSpeed.x*Time.time + startAnimOffset));
             scale.y = origScale.y * (1+scaleAmplitude.y*Mathf.Sin(scaleSpeed.y*Time.time + startAnimOffset));
             scale.z = origScale.z * (1+scaleAmplitude.z*Mathf.Sin(scaleSpeed.z*Time.time + startAnimOffset));
             transform.localScale = scale;
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 arielfel · Apr 02, 2019 at 09:43 AM 0
Share

Bro, you are the reason for my love for this community!. Thanks! 2 years later - still the first choice for my google.

BTW - I realy appreciate the words at the start in your comment, I'm also trying to reach into the lost souls of unity script explorers... by publishing cool generic scripts in a shape of " cut- 5 years later, here is my version for this ... :)"

BTW - if anyone looking for cool rotation script. here is a link: https://forum.unity.com/threads/simple-rotation-script-free.510303/

  • 1
  • 2
  • ›

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

12 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

Related Questions

Turret floats away and pushes everything away from it 2 Answers

Keep an object on the gound. 2 Answers

How Do You To Make An Object Shoot A Projectile? 1 Answer

2d axis is not shown 1 Answer

Hide GUI Label when click in open space or on other objects 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