我如何随机调用函数统一C# [英] How do I call functions randomly unity c#

查看:45
本文介绍了我如何随机调用函数统一C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款无尽的汽车驾驶游戏,我想在玩家的汽车靠近敌方汽车之一时随机调用switchLane函数之一.关键是要在接近时使其他一些汽车切换到随机车道.我主要要寻找的是一种在每次接近时随机调用这4个switchLane函数之一的方法.

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;
     }

 }
}

推荐答案

例如

// for more flexibility rather use an array
// You should also add the ability to change it via the Inspector
[SerializeField]
private float[] lanes = new []
{
    7.25f,
    2.45f,
    2.4f,
    7.3f
};

// pass the index of the lane to your method
// this way you can handle all lanes with a single method
private void SwitchToLane(int index)
{
    Debug.Log($"Switching to lane {index}", this);

    // Get the float at given index in lanes
    var x = lanes[index];

    var targetPos = new Vector3(x, 0, 0);
    transform.position += (targetPos * speed * Time.deltaTime);

    if (transform.position.x <= -x)
    {
       var newPositionLeft = new Vector3(-x, transform.position.y, transform.position.z);
       transform.position = newPositionLeft;
    }
    // Rather use 'else if' when it is already clear that only one case can be true at the same time
    else if (transform.position.x >= x)
    {
       var newPositionRight = new Vector3(x, transform.position.y, transform.position.z);
       transform.position = newPositionRight;
    }
}

然后致电

// Call this to switch to any random lane
private void SwitchToRandomLane()
{
    // generate a random int between 0 and lanes.Length - 1
    // second parameter is EXCLUSIVE!
    var random = Random.Range(0, lanes.Length); 

    SwitchToLane(random);
}
        


其他注意事项:

  1. 您还需要以某种方式在 Update 中更改条件检查.目前,只要玩家一次到达距离阈值以下,就可以切换车道每帧!

  1. You would also need to somehow change your condition check in Update. Currently as soon as the player goes once below the distance threshold you switch lane every frame!

您可能想在某个地方存储当前车道索引,因此当调用 SwitchToRandomLane 时,当前索引将从选项中排除,您只能肯定地切换到与该车道不同的车道你已经上线了.

You would probably want to somewhere store the current lane index so when SwitchToRandomLane is called the current index is excluded from the options and you can only definitely switch to a different lane then the one you are already on.

这两点我都留给你.

这篇关于我如何随机调用函数统一C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆