我知道这是一个很常见的问题,但是在我运气不佳的情况下尝试了解决方法。试图使某些对象以统一的方式发生冲突,但是在发生碰撞后不会进行OnCollision2d调用。希望能得到有关为什么发生这种情况的帮助。
此处提供了两个类
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Rigidbody2D))]
public class Missile: MonoBehaviour
{
public bool dead = false, hostile = true;
public float speed = 30, rotatingSpeed= 200;
public GameObject target;
private BoxCollider2D bc;
private Rigidbody2D rb;
private SpriteRenderer sr;
private GameObject go;
public Missile() { Debug.Log("Constructed"); }
private void OnEnable()
{
Debug.Log("Missile Launched");
go = new GameObject("Missile");
go.tag = "Missile";
target = GameObject.FindGameObjectWithTag("Ship");
rb = go.AddComponent<Rigidbody2D> ();
sr = go.AddComponent<SpriteRenderer> ();
bc = go.AddComponent<BoxCollider2D>();
bc.size = new Vector2(2f, 2f);
//bc.isTrigger = true;
go.transform.position = new Vector3(0,0, 11);
sr.sprite = SpriteManager.current.GetSprite("Ships", "missile1");
}
private void FixedUpdate()
{
//sr.sprite = SpriteManager.current.GetSprite("Missiles", "missile1");
//Debug.Log(target);
if (!target) return;
var point2Target = (Vector2) go.transform.position - (Vector2) target.transform.position;
point2Target.Normalize();
float value = Vector3.Cross(point2Target, go.transform.right).z;
rb.angularVelocity = rotatingSpeed * value;
//Vector2 lastVel = rb.velocity;
rb.velocity = go.transform.right * speed;
//rb.AddForce(go.transform.right * speed);
}
public void setPosition(int x, int y)
{
go.transform.position = new Vector3(x,y,11);
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
}
第二班
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Rigidbody2D))]
public class Ship: MonoBehaviour
{
public int hull = 100, type = 1;
public bool dead = false;
public float speed = 5, rotatingSpeed= 200;
public GameObject target;
private BoxCollider2D bc;
private Rigidbody2D rb;
private SpriteRenderer sr;
private GameObject go;
public Ship() { Debug.Log("Constructor"); }
private void OnEnable()
{
Debug.Log("Ship Built");
go = new GameObject("Ship");
go.tag = "Ship";
//target = GameObject.FindGameObjectWithTag("Player");
rb = go.AddComponent<Rigidbody2D> ();
sr = go.AddComponent<SpriteRenderer> ();
bc = go.AddComponent<BoxCollider2D>();
bc.size = new Vector2(2f, 2f);;
bc.isTrigger = true;
go.transform.position = new Vector3(0,0,11);
sr.sprite = SpriteManager.current.GetSprite("Ships", "ship2");
//sr.sprite.transform.localScale = anyWidth;
//sr.sprite = SpriteManager.current.GetSprite("Missiles", "missile1");
}
void FixedUpdate ()
{
//Debug.Log(target);
GameObject[] missiles = GameObject.FindGameObjectsWithTag("Missile");
/*foreach (GameObject missile in missiles)
{
Vector2 point2Target = ((Vector2)go.transform.position - (Vector2) missile.transform.position);
if(point2Target.magnitude < 50)
{
point2Target.Normalize();
Flak flak = gameObject.AddComponent<Flak>();
flak.setPosition(go.transform.position);
point2Target.x = point2Target.x * -1;
point2Target.y = point2Target.y * -1;
flak.setVelocity(point2Target * 50);
break;
}
}*/
if (target != null)
{
var point2Target = (Vector2) go.transform.position - (Vector2) target.transform.position;
point2Target.Normalize();
float value = Vector3.Cross(point2Target, go.transform.right).z;
rb.angularVelocity = rotatingSpeed * value;
rb.velocity = go.transform.right * speed;
//Vector2 lastVel = rb.velocity;
//rb.AddForce(go.transform.right * speed;);
}
}
public void setPosition(int x, int y) { go.transform.position = new Vector3(x,y,11); }
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
}
最后是调用
if (Input.GetKeyUp("i"))
{
var ship = gameObject.AddComponent<Ship>();
ship.setPosition((int)currFramePosition.x, (int)currFramePosition.y);
}
if (Input.GetKeyUp("v"))
{
var missile = gameObject.AddComponent<Missile>();
missile.setPosition((int)currFramePosition.x, (int)currFramePosition.y);
Debug.Log(missile);
}
感谢您的帮助。