Search Unity

Platform move left and right randomly

Discussion in 'Scripting' started by Keiji1, Oct 22, 2021.

  1. Keiji1

    Keiji1

    Joined:
    Jul 8, 2021
    Posts:
    4
    Hi, I am pretty new to Unity and I am trying to make a platform jumping game. I got this script working as it should from a Youtube video but I wanted to make the platform move randomly because I have multiple platforms that gonna spawn when I start the game. So basically when the platform spawn 1 should go from left to right and the other should go from right to left.

    Code (CSharp):
    1. public Transform pos1, pos2;
    2. public float speed;
    3. public Transform startPos;
    4.     Vector2 nextPos;
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         nextPos = startPos.position;
    9.     }
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if(transform.position == pos1.position)
    14.         {
    15.             nextPos = pos2.position;
    16.         }
    17.         if(transform.position == pos2.position)
    18.         {
    19.             nextPos = pos1.position;
    20.         }
    21.         transform.position = Vector2.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    22.     }
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Since you give 2 points where the platform moves between just let the other start at the opposite side (so exchange the points). And I mean not in code but the "waypoint" gameobjects you drag into the inspector fields of each platform.

    It's preferable to have the same code run/react to different input data than to have to manage such stuff in code since that tends to get really complex fast and is also harder to debug/tune/balance.

    Does that help?`If not you should specify more what exactly you need help with in your code.
     
    Keiji1 likes this.
  3. Keiji1

    Keiji1

    Joined:
    Jul 8, 2021
    Posts:
    4
    I guess this is also a way to do it. But basically, I wanted to have the platform start from a random point from the 2 positions then they can go from left to right or right to left. For example, from pos1 = 1.5f and pos2 = -1.5f, I wanted the platform to start/spawn in a random range between these 2 pos and then move left to right or right to left.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Ah I see.
    https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html may help you.
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.    transform.position = Vector3.Lerp(pos1, pos2, Mathf.value);
    5.    if(Mathf.value > 0.5f)
    6.    {
    7.        nextpos = pos1;
    8.    }
    9.    else
    10.    {
    11.        nextpos = pos2;
    12.    }
    13. }
    14.  
    Note that Mathf.value returns a pseudorandom value between 0.0f and 1.0f.
     
    Keiji1 likes this.