[这里,在我的代码中使用C#中的Microsoft Visual Studio进行游戏开发以检测Collision。我编写了脚本,以使用A *算法查找播放器的最短路径。在此脚本中,我无法检测到不同玩家之间的冲突,我想降低一个玩家的速度并提高另一个玩家使用不同标签的速度。但是我无法应付播放器的速度。
这是我的代码
**using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PathfindingTester: MonoBehaviour {
// The A* manager.
private AStarManager AStarManager = new AStarManager();
// Array of possible waypoints.
List < GameObject > Waypoints = new List < GameObject > ();
// Array of waypoint map connections. Represents a path.
List < Connection > ConnectionArray = new List < Connection > ();
// The start and end target point.
public GameObject start;
public GameObject end;
// Debug line offset.
Vector3 OffSet = new Vector3(0, 0.3 f, 0);
public float speed;
private Rigidbody rb;
private Transform target;
int current;
//float WPradius = 0.5f;
Connection aConnection;
public Text countText;
private int countit;
//public GameObject[] targets;
// Start is called before the first frame update
void Start() {
if (start == null || end == null) {
Debug.Log("No start or end waypoints.");
return;
}
// Find all the waypoints in the level.
GameObject[] GameObjectsWithWaypointTag;
GameObjectsWithWaypointTag = GameObject.FindGameObjectsWithTag("Waypoint");
foreach(GameObject waypoint in GameObjectsWithWaypointTag) {
WaypointCON tmpWaypointCon = waypoint.GetComponent < WaypointCON > ();
if (tmpWaypointCon) {
Waypoints.Add(waypoint);
}
}
// Go through the waypoints and create connections.
foreach(GameObject waypoint in Waypoints) {
WaypointCON tmpWaypointCon = waypoint.GetComponent < WaypointCON > ();
// Loop through a waypoints connections.
foreach(GameObject WaypointConNode in tmpWaypointCon.Connections) {
Connection aConnection = new Connection();
aConnection.SetFromNode(waypoint);
aConnection.SetToNode(WaypointConNode);
AStarManager.AddConnection(aConnection);
}
}
// Run A Star...
ConnectionArray = AStarManager.PathfindAStar(start, end);
// Debug.Log(ConnectionArray.Count);
// rb = GetComponent<Rigidbody>();
//rb.MovePosition((ConnectionArray[0].GetFromNode().transform.position + OffSet));
//transform.position = ConnectionArray[0].GetFromNode().transform.position;
countit = 0;
countText.text = "Count: " + countit;
/*countTextTwo.text = "Counttwo:" + countit;
countTextThree.text = "Countthree:" + countit;*/
}
// Draws debug objects in the editor and during editor play (if option set).
void OnDrawGizmos() {
// Draw path.
foreach(Connection aConnection in ConnectionArray) {
Gizmos.color = Color.red;
Gizmos.DrawLine((aConnection.GetFromNode().transform.position + OffSet),
(aConnection.GetToNode().transform.position + OffSet));
}
}
// Update is called once per frame
void Update() {
if (transform.position != ConnectionArray[current].GetToNode().transform.position) {
Vector3 pos2 = Vector3.MoveTowards(transform.position, ConnectionArray[current].GetToNode().transform.position, speed * Time.deltaTime);
var LookPos = ConnectionArray[current].GetToNode().transform.position - transform.position;
LookPos.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(LookPos), 1);
GetComponent < Rigidbody > ().MovePosition(pos2);
//Debug.Log(transform.position);
} else {
current = (current + 1) % ((ConnectionArray.Count));
// if (current + 2 == (ConnectionArray.Count - 1) && (transform.position != ConnectionArray[current].GetToNode().transform.position))
if (current + (ConnectionArray.Count - 1) == (ConnectionArray.Count - 1) && (transform.position != ConnectionArray[current].GetToNode().transform.position)) {
if ((transform.position != ConnectionArray[(current + (ConnectionArray.Count - 1))].GetFromNode().transform.position)) {
countit = countit + 1;
countText.text = "Count:" + countit;
/*countTextTwo.text = "Counttwo:" + countit;
countTextThree.text = "Countthree:" + countit;*/
speed = speed - 1 f;
}
ConnectionArray.Reverse();
Vector3 pos3 = Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed * Time.deltaTime);
GetComponent < Rigidbody > ().MovePosition(pos3);
if (countit == 10) {
speed = 0 f;
}
} else {
{
current = (current) % ((ConnectionArray.Count));
}
}
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("target")) {
other.gameObject.SetActive(true);
countit = countit + 1;
countText.text = "Count: " + countit;
}
if (other.gameObject.CompareTag("playerfirstspeed")) {
other.gameObject.SetActive(true);
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed * Time.deltaTime);
}
if (other.gameObject.CompareTag("playersecondspeed")) {
other.gameObject.SetActive(true);
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed * Time.deltaTime);
}
if (other.gameObject.CompareTag("playerthirdspeed")) {
other.gameObject.SetActive(true);
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed * Time.deltaTime);
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.CompareTag("target")) {
other.gameObject.SetActive(false);
}
if (other.gameObject.CompareTag("playerfirstspeed")) {
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed - 2);
}
if (other.gameObject.CompareTag("playersecondspeed")) {
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed - 2);
}
if (other.gameObject.CompareTag("playerthirdspeed")) {
Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed - 2);
}
}
}**
据我所知,对于三个玩家,您只有一个变量“速度”。
如何为每个玩家声明“速度”变量,然后检查某些半径是否与其他玩家发生冲突,通过碰撞让其他玩家获取并对其进行加法或减法?