如何随机调用函数unity c# [重复]

Posted

技术标签:

【中文标题】如何随机调用函数unity c# [重复]【英文标题】:How do I call functions randomly unity c# [duplicate] 【发布时间】:2020-12-26 21:25:59 【问题描述】:

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


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;
     

 

【问题讨论】:

【参考方案1】:

例如呢

// 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);

        

补充说明:

    您还需要以某种方式更改Update 中的条件检查。目前,一旦玩家低于距离阈值一次,您每帧都会切换车道!

    您可能希望在某处存储当前车道索引,因此当调用 SwitchToRandomLane 时,当前索引将被排除在选项之外,您只能绝对切换到与您已经在的车道不同的车道。

这两点我留给你。

【讨论】:

【参考方案2】:

你可以做一个随机数

int randomNum = Random.Range(0, 4);

并在具有四个功能的switch 上使用它,就像这样

switch(randomNum)
case 0:
  switchLane1();
break;
case 1:
  switchLane2();
break;
case 2:
  switchLane3();
break;
case 3:
  switchLane4();
break;

但是我相信有更好的解决方案,我看到这么多重复的代码,也许你可以为四种情况制作一个带有参数的函数,然后简单地发送一个带有方向或数字的参数。

【讨论】:

这有点像我正在寻找的东西,但是为了让它在我每次靠近汽车时都能工作,这个代码只能工作一次......关于删除重复的代码,我是一个完整的初学者所以我真的不知道我应该怎么做,所以这是我想出的解决方案:)(游戏在无限循环中运行,所以你多次通过汽车) @Lukaas 只发生一次的原因是“enemyCarPrefab”总是相同的,而“if”只在一个地方为真。我认为您应该进行更多研究并按照基本教程进行培训,您有 Unity 课程和小游戏,所有这些都解释了。 learn.unity.com

以上是关于如何随机调用函数unity c# [重复]的主要内容,如果未能解决你的问题,请参考以下文章

unity c#怎么调用析构函数

如何在 Unity/C# WebGL 平台中调用 async/await JavaScript 函数?

如何从java调用C#函数[重复]

在C#后面调用Javascript函数[重复]

C# Unity 下拉菜单,更改调用函数,函数查询列表返回匹配列表项

Unity3D如何调用另外一个脚本里的函数呢