- Home /
Question by
z4zion · Aug 06, 2017 at 07:43 AM ·
collisioncollision2d
Unable to detect collision in 2d game please help!!
i have a rocket and some planets, i have attached colliders on both and both are rigid bodies. but i am not able to detect collisions. here is the script .PLEASE HELP!! using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class RocketController : MonoBehaviour {
public float speed;
private Rigidbody2D rgbd;
// Use this for initialization
void Start () {
rgbd = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"),
CrossPlatformInputManager.GetAxis("Vertical"));
Debug.Log(moveVec);
Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"),
CrossPlatformInputManager.GetAxis("Vertical"), 4000);
transform.rotation = Quaternion.LookRotation(lookVec, Vector3.back);
transform.Translate(moveVec * Time.deltaTime * speed, Space.World);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag=="planet")
{
Destroy(collision.gameObject);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612132040im_/https://answers.unity.com/themes/thub/images/avi.jpg)