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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Move object up and down and at different heights

Discussion in 'Scripting' started by Cheeko25, Jun 29, 2020.

  1. Cheeko25

    Cheeko25

    Joined:
    Jun 29, 2020
    Posts:
    8
    Hey there, I'm brand spanking new to all of this and really having a hard time. I have an object that's scrolling left into the scene. I need the object to both appear at random heights as well as move up and down. I am able to get them to work independently but not together.

    Code (CSharp):
    1. void Update()
    2.         {
    3.             GameObject newitem = Instantiate(item);
    4.             newitem.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 0);
    5.         }
    How would I use mathf.pingpong in conjunction with this?
    I'm assuming because they are both trying to change the y axis one or the other can only work.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    First, welcome!

    Second, you are instantiating a new item every single frame, since Update() runs every frame. That's like 60 objects per second. I'm not sure you intend that. Here's a neat timing diagram:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    As for this particular problem, break down the steps of what you want, as there are many different ways to interpret what you say above.

    Also, it may help to think of it in terms of an existing popular game, and you would be amazed at how many youtube video tutorials there are out there to do exactly the mechanics of a popular game inside of Unity.
     
  3. Cheeko25

    Cheeko25

    Joined:
    Jun 29, 2020
    Posts:
    8
    Hey there, thanks for the response.

    Yes, I did want a new object to appear. I have them on a timer and destroyed after a set time.

    As for the main issue, I just can't seem to figure it out. I've spent days going through tutorials and it hasn't helped. :(