I am making an endlesss car driving game and I want to call one of the switchLane functions by random whenever the player car is close to one of the enemy cars. The point is to make some other cars switch to a random lane when you get close. The main thing I am looking for is a way to call one of these 4 switchLane functions by random everytime you get close.
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();
switchLane2();
switchLane3();
switchLane4();
}
}
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;
}
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;
}
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;
}
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;
}
if (transform.position.x >= lane4)
{
Vector3 newPositionRight = new Vector3(lane4, transform.position.y, transform.position.z);
transform.position = newPositionRight;
}
}
}
Aucun commentaire:
Enregistrer un commentaire