void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag == "Enemy1" ) {
enemyContact = true;
Debug.Log ("shot has collided with tagged enemy");
}
}
问题是我知道碰撞是成功的,因为调试显示,但是敌人的联系不会变为真。一旦确实敌人死亡,如果我在清醒中将其设置为真,则证明这是真的。
但是如何,程序可以进入调试但仍然拒绝将布尔值变为true ???
我一定是在失去理智。
我也试过了:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy1")
{
enemyContact = true;
Debug.Log ("trigger enter worked");
}
}
变量不会改变。同样,即使调试日志显示在联系人上,该变量也不会改变。
我也尝试过:
void OnTriggerEnter2D(Collider2D CollisionCheck)
{
if (CollisionCheck.gameObject.tag == "Enemy1") {
Debug.Log ("shot has collided with tagged enemy");
enemyContact = true;
}
}
变量不会变为true。无论。 甚至尝试过:
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag == "Enemy1" ) {
Debug.Log ("shot has collided with tagged enemy");
if (enemyContact == false){
enemyContactFunction();
}
}
void enemyContactFunction(){
enemyContact = true;
}
调试再次关闭,但函数不会运行,变量保持不变。变量是公开的,我可以看。我不知道还有什么要补充的。这是整个脚本:
using UnityEngine;
using System.Collections;
public class ShotScript : MonoBehaviour {
public Player pScript;
public Vector2 velocityRight = new Vector2(5, 0);
public Vector2 velocityLeft = new Vector2(-5, 0);
public Vector2 velocityUp = new Vector2(0, 5);
public Vector2 velocityDown = new Vector2(0,-5);
public Vector2 velocityupRight = new Vector2(5, 5);
public Vector2 velocityupLeft = new Vector2(-5, 5);
public Vector2 velocitydownRight = new Vector2(5,-5);
public Vector2 velocitydownLeft = new Vector2(-5,-5);
private Rigidbody2D shotRigid;
public bool enemyContact = false;
public bool enemyContactTrue = false;
void Awake()
{
shotRigid = GetComponent<Rigidbody2D> ();
}
void Start()
{
//checking which direction player is facing to decide which way to shoot
shotRigid = GetComponent<Rigidbody2D> ();
}
void enemyContactFunction(){
enemyContact = true;
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag == "Enemy1" ) {
Debug.Log ("shot has collided with tagged enemy");
if (enemyContact == false){
enemyContactFunction();
}
}
} }