-1

I am making a car game where the enemies have behaviour where to move to a specefic point on the road I already have the 4 points but I don't know how to randomize where the enemy prefab moves to I have to change it manually now. I would really appreciate some help and thank you already. I am a beginner at coding so take it easy ;)

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public float speed;
    private GameObject playerTransform;
    private GameObject enemyCarPrefab;
    private float distanceToNextCar;
    public float distance;

    private float roadWidth;
    private float lane1 = 7.25f;
    private float lane2 = 2.45f;
    private float lane3 = 2.4f;
    private float lane4 = 7.3f;


    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        playerTransform = GameObject.FindGameObjectWithTag("Player");
        enemyCarPrefab = GameObject.FindGameObjectWithTag("BaseCar");

        distanceToNextCar = enemyCarPrefab.transform.position.z - distance;
        if (playerTransform.transform.position.z > distanceToNextCar)
        {
            switchLane1();
        }
    }


    private void switchLane1()
    {
        Vector3 targetPos1 = new Vector3(-lane1, 0, 0);
        transform.position += (targetPos1 * speed * Time.deltaTime);



        if (transform.position.x <= -lane1)
        {
            Vector3 newPositionLeft = new Vector3(-lane1, transform.position.y, transform.position.z);
            transform.position = newPositionLeft;
        }
        else if (transform.position.x >= lane1)
        {
            Vector3 newPositionRight = new Vector3(lane1, transform.position.y, transform.position.z);
            transform.position = newPositionRight;
        }

    }

    private void switchLane2()
    {
        Vector3 targetPos2 = new Vector3(-lane2, 0, 0);
        transform.position += (targetPos2 * speed * Time.deltaTime);



        if (transform.position.x <= -lane2)
        {
            Vector3 newPositionLeft = new Vector3(-lane2, transform.position.y, transform.position.z);
            transform.position = newPositionLeft;
        }
        else if (transform.position.x >= lane2)
        {
            Vector3 newPositionRight = new Vector3(lane2, transform.position.y, transform.position.z);
            transform.position = newPositionRight;
        }

    }

    private void switchLane3()
    {
        Vector3 targetPos3 = new Vector3(lane3, 0, 0);
        transform.position += (targetPos3 * speed * Time.deltaTime);



        if (transform.position.x <= -lane3)
        {
            Vector3 newPositionLeft = new Vector3(-lane3, transform.position.y, transform.position.z);
            transform.position = newPositionLeft;
        }
        else if (transform.position.x >= lane3)
        {
            Vector3 newPositionRight = new Vector3(lane3, transform.position.y, transform.position.z);
            transform.position = newPositionRight;
        }

    }

    private void switchLane4()
    {
        Vector3 targetPos4 = new Vector3(lane4, 0, 0);
        transform.position += (targetPos4 * speed * Time.deltaTime);



        if (transform.position.x <= -lane4)
        {
            Vector3 newPositionLeft = new Vector3(-lane4, transform.position.y, transform.position.z);
            transform.position = newPositionLeft;
        }
        else if (transform.position.x >= lane4)
        {
            Vector3 newPositionRight = new Vector3(lane4, transform.position.y, transform.position.z);
            transform.position = newPositionRight;
        }

    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Lukaas
  • 19
  • 5
  • 1
    I think you are missing the whole point of using functions here... What you should have done already is having one function `private void switchLane(float lane)` and then call `switchLane(lane1)` or `switchLane(lane2)` etc. Now, what you want to do is removing that parameter, having an array of lanes at the beginning of your function, and using `Math.Random` to chose an index – Rafalon Sep 09 '20 at 08:07
  • 1) I don't understand what you want to achieve, can you be more specific? Give some example? 2) You could use an unique switchLane(float lane) function with a parameter instead of 4 functions doing the same thing. – Cirrus Minor Sep 09 '20 at 08:09
  • I want to achieve enemy behaviour by them switching lanes when getting close to the player. I now made the switchLane(float lane) but I am still not sure how to make the enemy switch to one of the 4 lanes randomly – Lukaas Sep 09 '20 at 08:15
  • **[Are you ignoring me ? :D](https://stackoverflow.com/questions/63794814/how-do-i-call-functions-randomly-unity-c-sharp/63794888#63794888)** ... which is by the way exactly the same thing people answered here again ... please stick to one question and don't post duplicates of your own one – derHugo Sep 09 '20 at 08:32

3 Answers3

0

You can use Random class in C# to generate a random value between 1 and 4, a switch statement can call any of the method based on the random value like below

var lane = new Random().Next(1, 5);
switch (lane)
{
    case 1:
       switchLane1();
       break;
    case 2:
       switchLane2();
       break;
    case 3:
       switchLane3();
       break;
    case 4:
       switchLane4();
       break;
    default:
       break;
}
Vimal CK
  • 3,543
  • 1
  • 26
  • 47
0

This is a little standalone program that will help you understand how to achieve what you want to achieve:

using System;

class Program
{
    static void Main()
    {
        // Here are all the lanes in an array
        var lanes = new float[]{7.25f, 2.45f, 2.4f, 7.3f};
        // Here is a random number generator
        var rand = new Random();
        
        // This will call 'switchLane' with one lane picked at random
        switchLane(lanes[rand.Next(0,lanes.Length)]);
    }
    
    static void switchLane(float lane)
    {
        // This displays the lane, change to whatever you want to do with it
        Console.WriteLine(lane);
    }
}
Rafalon
  • 4,450
  • 2
  • 16
  • 30
0

Since a "lane" is such an important concept in your program, I'd give it its own class. Might look like this:

class Lane
{
    public float Position { get; }

    public Lane(float position) => this.Position = position;
    
    public MoveInto(GameObject objectToMove)
    {
        Vector3 targetPos1 = new Vector3(this.Position, 0, 0);
        objectToMove.Position += (targetPos1 * speed * Time.deltaTime);

        if (objectToMove.Position <= this.Position)
        {
            //etc....
        }
    }
}

Then in your EnemyScript class you'd keep a list of these:

class EnemyScript : MonoBehavior
{
    private Lane[] lanes = new Lane[]  
    {
        new Lane(7.25f),
        new Lane(2.45f),
        new Lane(2.4f),
        new Lane(7.3f)
    };

Then to switch to one of the lanes, you'd just do this:

lanes[laneNumber - 1].MoveInto(player);
John Wu
  • 50,556
  • 8
  • 44
  • 80