Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make an object move up and down on a loop.

Discussion in 'Scripting' started by PVisser, Jan 17, 2016.

Thread Status:
Not open for further replies.
  1. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    Hello dear Unity community,

    For the past few hours I've been trying to get this seemingly simple thing done. I am by no means a programmer, but because I'm trying to create a prototype I have to make some scripts. I have been Googling and reading documentations but every time I think I have the solution the compiler gives me al sorts of errors.

    In short: I want to have a game object (Collectible) move up and down, in a loop, and me to be able to change the speed and height in the editor. I imagine this being something very simple, but this programming thing goes above my head. Could someone help me with this?



    As a side note there are probably going to be more instances in which I need to make small scripts, if someone could keep helping me as sort of a mentor/helping programmer I'd appreciate that a lot. At this point I'm just prototyping, but in case this turns into something I want to pursue I'm willing to share revenue. :)
     
  2. Diversity276

    Diversity276

    Joined:
    Mar 22, 2014
    Posts:
    51
  3. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    Thanks, i'll look into this tomorrow. Time for me to get some sleep and take a fresh look at it again in the morning.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you could try an animationCurve

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestScript : MonoBehaviour
    6. {
    7.     public AnimationCurve myCurve;
    8.    
    9.     void Update()
    10.     {
    11.         transform.position = new Vector3(transform.position.x, myCurve.Evaluate((Time.time % myCurve.length)), transform.position.z);
    12.     }
    13. }
    14.  
    you can setup whatever curve you like then (note the little cogs each end of the curve when you edit it, select "loop" :) )
     
  5. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    You could use a bit of trig which will make it SUPER easy for you to edit the speed and height of the animation.

    So in a script you attach to the object, put something like this:

    Code (csharp):
    1.  
    2. //adjust this to change speed
    3. float speed = 5f;
    4. //adjust this to change how high it goes
    5. float height = 0.5f;
    6.  
    7. void Update() {
    8.         //get the objects current position and put it in a variable so we can access it later with less code
    9.         Vector3 pos = transform.position;
    10.         //calculate what the new Y position will be
    11.         float newY = Mathf.Sin(Time.time * speed);
    12.         //set the object's Y to the new calculated Y
    13.         transform.position = new Vector3(pos.x, newY, pos.z) * height;
    14. }
    15.  
     
  6. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
  7. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    Thank you LeftyRighty, your solution is wonderful and does what I wanted. Also my thanks to Diericx and qqqbbb for helping. :)
     
  8. Nitro00

    Nitro00

    Joined:
    Jan 11, 2018
    Posts:
    18
    Quick question, how do I make it so that it floats around its current Y axis. I have this modified slightly to be used in a 2D game, but I cannot get it to move around on its current position.
     
  9. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    Rollerball Tutorial my friend, short, simple and you will be suprised how much you will learn. Its in the Unity home page under the learn section.
     
  10. Nitro00

    Nitro00

    Joined:
    Jan 11, 2018
    Posts:
    18
    Yah I've already seen it but just realized all I had to do was change it to transform.localPosition :p
     
  11. JoaoBM

    JoaoBM

    Joined:
    Sep 23, 2018
    Posts:
    15
    Although old, for anyone in the future that might bump into this post, there are 2 issues with this little block of code.
    First , since you are multiplying the position by the height, the position will be changed. Not just the Y. Say we have an object at coordinates (2, 2, 2). When this code runs, given that the height = 0.5, the object will be moved to (1, 1, 1).
    Instead, to control the height of the up and down movement, you should be multiplying the variable by the newY.

    Also the newY will will be going around the 0 coordinate. To fix this you should add the old position to the newY

    float newY = Mathf.Sin(Time.time * speed) * height + pos.y;
     
    providejahwill and GoStudios_ like this.
  12. hassonhamo3

    hassonhamo3

    Joined:
    May 25, 2018
    Posts:
    38
    here goes the right edited one :



    //adjust this to change speed
    [SerializeField]
    float speed = 5f;
    //adjust this to change how high it goes
    [SerializeField]
    float height = 0.5f;

    Vector3 pos;

    private void Start()
    {
    pos = transform.position;
    }
    void Update()
    {

    //calculate what the new Y position will be
    float newY = Mathf.Sin(Time.time * speed) * height + pos.y;
    //set the object's Y to the new calculated Y
    transform.position = new Vector3(transform.position.x, newY, transform.position.z) ;
    }
     
  13. Badbanshee

    Badbanshee

    Joined:
    Feb 13, 2020
    Posts:
    1
    Thank you my man
     
  14. GiacoGrecoMelon

    GiacoGrecoMelon

    Joined:
    May 15, 2020
    Posts:
    1
    Thank you
     
  15. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14
    what if you want to stop movement on key? lets say down "n", and remember last position?, just stopping not reseting
     
  16. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    33
    @LeftyRightyWhat a simple solution, it works so well. I almost feel like there must be a major downside to this method since I've never seen this method before... But maybe I was looking in the wrong directions. Thank you!

    edit: I was looking for a way to make my floating object slightly 'hover' up and down in the air. With this curve solution I even have full control over the movement...
     
  17. Epicdragon66890

    Epicdragon66890

    Joined:
    Jan 28, 2022
    Posts:
    1
    How would I do the same thing except when you jump on (in my case a cloud) it bobs down then back up when you first touch it?
     
  18. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    136
    Four years, still great
     
  19. SemihG

    SemihG

    Joined:
    Feb 14, 2021
    Posts:
    2
    This still works! Thanks
     
  20. sam_nau

    sam_nau

    Joined:
    Oct 26, 2013
    Posts:
    9
    Thank you so much. This is exactly what I needed.
     
  21. RehanBhutta007

    RehanBhutta007

    Joined:
    Sep 3, 2020
    Posts:
    9
    Thanks man,,,,,
     
Thread Status:
Not open for further replies.