如何解决有没有一种方法可以将四个switchLane功能变成一个功能,并随机选择它切换到哪个通道?
我正在做一个汽车游戏,敌人有行为要移动到道路上的特定点,我已经有4个点,但是我不知道如何随机化敌人预制件向何处移动,我必须更改它现在手动。我将非常感谢您的帮助,并已经感谢您。我是编码的初学者,请放轻松;)
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);
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.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;
}
else 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;
}
else 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;
}
else if (transform.position.x >= lane4)
{
Vector3 newPositionRight = new Vector3(lane4,transform.position.z);
transform.position = newPositionRight;
}
}
}
解决方法
您可以在C#中使用Random
类来生成1到4之间的随机值,switch
语句可以基于如下所示的随机值调用任何方法
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;
}
,
这是一个小小的独立程序,可以帮助您了解如何实现想要实现的目标:
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);
}
}
,
由于“车道”在您的程序中是如此重要,因此我将为其提供自己的类。可能看起来像这样:
class Lane
{
public float Position { get; }
public Lane(float position) => this.Position = position;
public MoveInto(GameObject objectToMove)
{
Vector3 targetPos1 = new Vector3(this.Position,0);
objectToMove.Position += (targetPos1 * speed * Time.deltaTime);
if (objectToMove.Position <= this.Position)
{
//etc....
}
}
}
然后在您的EnemyScript类中,保留以下列表:
class EnemyScript : MonoBehavior
{
private Lane[] lanes = new Lane[]
{
new Lane(7.25f),new Lane(2.45f),new Lane(2.4f),new Lane(7.3f)
};
然后切换到其中一条车道,只需执行以下操作:
lanes[laneNumber - 1].MoveInto(player);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。