- Home /
How do I get my enemy to destroy my spaceship?
,Basically I have a spaceship and an enemy. When my spaceship comes in to contact with the enemy i want them to both be destroyed. However, whats happening is, the player is just going straight through the enemy.
Any solution would be greatly appreciated! :D (I've attached the scripts i used for both the enemy and the spaceship) alt text
@OrthorsOfficial please post your code by clicking the "101010" button on the toolbar, ins$$anonymous$$d of images. This helps copy/paste when others modify your code to help you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy (other.gameObject);
Destroy (gameObject);
}
}
}
Answer by AcePilot10 · Jan 09, 2018 at 09:41 PM
A few things could be the issue here. For us to help you it would probably be useful to post a screenshot of your collider component. In the meantime make sure that your collider is set to IsTrigger (As you are checking for OnTriggerEnter). Also try some debugging like seeing if the collision is triggered by writing Debug.Log("Ship was hit")
inside of your OnTriggerEnter function
Answer by OrthorsOfficial · Jan 09, 2018 at 09:59 PM
@AcePilot10 Hey thanks for the help. Ive just attached the screenshots. I tried adding in the "Debug" but I dont think anything is being triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed;
public float clockwise = 100.0f;
public float counterClockwise = 5.0f;
private Rigidbody2D rb2d;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
rb2d.transform.Translate (0, speed * Time.deltaTime, 0);
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
{
transform.Rotate (0, 0, Time.deltaTime * clockwise);
}
else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S))
{
transform.Rotate (0, 0, -Time.deltaTime * counterClockwise);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy (other.gameObject);
Destroy (gameObject);
}
}
}