In my game the character throws a rock at enemies, however the process of the rock colliding with the enemy does not seem to work. Please help me!
void OnTriggerEnter2D(Collision other)
{
if(other.transform.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
},In my game, I throw a rock and try to hit an enemy, however the collision detection between the rock and the enemy does not seem to work. Please help me!
void OnTriggerEnter2D(Collision other)
{
if(other.transform.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
OnTriggerEnter2D uses ‘Collider2D’ not ‘Collision’
if you change your script to this it should work:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
The rock now doesn’t seem to fire - I don’t know why.
public Transform firePoint;
public GameObject rock;
if (Input.GetKeyDown(KeyCode.LeftShift))
{
Instantiate(rock, firePoint.position, firePoint.rotation);
}
public float speed;
public Movement player;
public GameObject Death;
// Use this for initialization
void Start () {
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
player = FindObjectOfType<Movement>();
if(player.transform.localScale.x < 0)
{
speed = -speed;
}
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
Now the problem is not so much the collision detection, but the actual firing of the rock. BTW thanks for the help.
@lawrence-parry