【发布时间】:2019-12-19 08:21:01
【问题描述】:
在这里,在我的代码中,使用 C# 中的 Microsoft Visual Studio 进行游戏开发以检测碰撞。我编写了脚本来使用 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.3f, 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 = 0f;
}
}
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);
}
}
}
【问题讨论】:
-
你如何选择谁可以加快速度,谁可以减慢速度?
-
这是问题...我无法解决这个问题
-
嗯,这是一个设计问题 - 你需要弄清楚你将如何决定谁慢谁快,我们谁都不应该为你决定
-
您的所有代码都与这个问题相关吗?它有点多,在当前的格式中很难解析
-
@derHugo 你有什么想法吗?如果一个玩家靠近另一个玩家,那么首先会改变它的方向吗?
标签: c# unity3d game-physics