单击某个对象后验证是否与该对象发生冲突
本文关键字:对象 冲突 验证 单击 是否 | 更新日期: 2023-09-27 18:24:04
如果我点击某个"航路点",我很难创建一种方法来检查玩家是否没有与该"航路点’碰撞。我已经设置了7个路点,玩家希望能够移动到这些路点。
现在,我正试图写一段脚本,检查在点击航路点后(在MouseDown上)是否与玩家发生碰撞。因为如果是这样的话,它就不会计算要移动到的位置。
public class WayPointPositioner : MonoBehaviour {
private Vector3 wayPointPosition;
public GameObject playercheck;
//Check if object is clicked
void OnMouseDown ()
{
Debug.Log ("Object Clicked " + GameObject.name);
// Check if collision occurs with playercheck
OnCollisionEnter(playercheck != Collision)
{
// If its the player, then return a new position for the player to move to for walking
// Else debug that its not so
if (playercheck.gameObject.CompareTag("Player"))
{
Debug.Log ("Object not colliding and retrieving position");
wayPointPosition = new Vector3 (GameObject.X, GameObject.Y, 10);
wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition);
}
else
{
Debug.Log ("Object is colliding, no movement needed");
}
}
}
}
现在我已经发现OnCollisionEnter
不起作用了。因为它需要先有void语句,然后才能运行。然而,我不知道我还能做什么。
我只想比较玩家的位置和终点。如果相等,则玩家在那里,否则移动到终点。希望我能正确理解这个问题。
设法修复了它。只是个白痴。
关键是分别使用这两种功能。因此,我将检查Mousedown和检查该位置是否被占用的任务进行了划分,方法是创建一个单独的变量,从中询问true或false,以检查所有内容。
public class WayPointPositioner : MonoBehaviour {
private Vector3 wayPointPosition;
private bool checkPlayerWaypointCollision;
void Start()
{
wayPointPosition = transform.position;
}
void OnTriggerStay2D (Collider2D other)
{
if (other.gameObject.name == "Player")
{
checkPlayerWaypointCollision = true;
}
else
{
checkPlayerWaypointCollision = false;
}
}
//Check if object is clicked
void OnMouseDown ()
{
// If its the player, then return a new position for the player to move to for walking
// Else debug that its not so
if (checkPlayerWaypointCollision == false)
{
Debug.Log ("Object not colliding and retrieving position");
transform.position = new Vector3 (transform.position.x, transform.position.y, 10);
wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition);
}
else
{
Debug.Log ("Object is colliding, no movement needed");
}
}
}