问题描述
我正在制作一款无尽的汽车驾驶游戏,我想在玩家的汽车靠近敌方汽车之一时随机调用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);
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.z);
transform.position = newPositionRight;
}
}
private void switchLane2()
{
Vector3 targetPos2 = new Vector3(-lane2,0);
transform.position += (targetPos2 * speed * Time.deltaTime);
if (transform.position.x <= -lane2)
{
Vector3 newPositionLeft = new Vector3(-lane2,transform.position.z);
transform.position = newPositionLeft;
}
if (transform.position.x >= lane2)
{
Vector3 newPositionRight = new Vector3(lane2,transform.position.z);
transform.position = newPositionRight;
}
}
private void switchLane3()
{
Vector3 targetPos3 = new Vector3(lane3,0);
transform.position += (targetPos3 * speed * Time.deltaTime);
if (transform.position.x <= -lane3)
{
Vector3 newPositionLeft = new Vector3(-lane3,transform.position.z);
transform.position = newPositionLeft;
}
if (transform.position.x >= lane3)
{
Vector3 newPositionRight = new Vector3(lane3,transform.position.z);
transform.position = newPositionRight;
}
}
private void switchLane4()
{
Vector3 targetPos4 = new Vector3(lane4,0);
transform.position += (targetPos4 * speed * Time.deltaTime);
if (transform.position.x <= -lane4)
{
Vector3 newPositionLeft = new Vector3(-lane4,transform.position.z);
transform.position = newPositionLeft;
}
if (transform.position.x >= lane4)
{
Vector3 newPositionRight = new Vector3(lane4,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);
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.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
时,当前索引将从选项中排除,您只能确定切换到与您已经使用的通道不同的通道上。
这两点我都留给你。
,您可以创建一个随机数
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;
}
但是我敢肯定有一个更好的解决方案,我看到了很多重复的代码,也许您可以针对这四种情况用一个参数来做一个函数,而只需发送一个带有方向或数字的参数即可。