I was able to solve the problem myself, but it was in a much different way that probably would not be helpful to others with the same type of question.
Help with making an object dodge other objects?
Hello, I'm very new here, and as my first project, I've been making an Asteroids style game. But I've come across a little problem.
I have an Alien object in the game that flies around and shoots at the player, but I am trying to get him to also try and dodge asteroids and the player bullets.
What I have is the Alien object, and that has a child object that is just a CircleCollider2D. If an object comes within the CircleCollider2D of the child object, it should cause the Alien to move in the opposite direction. But what actually happens is if an object collides with the CircleCollider2D, the Alien explodes.
The AlienScript is the code that goes directly on the Alien Object. That is here :
using UnityEngine;
using System.Collections;
public class AlienScript : MonoBehaviour {
public float MinForce = 20f;
public float MaxForce = 40f;
public float DirectionChangeInterval = 1f;
public float ShotInterval = 1f;
public GameObject AlienBullet;
public GameObject AlienExplosion;
private GameObject ship;
private float directionChangeInterval;
private float shotInterval;
// Use this for initialization
void Start ()
{
directionChangeInterval = DirectionChangeInterval;
shotInterval = ShotInterval;
Push();
}
// Update is called once per frame
void Update ()
{
directionChangeInterval -= Time.deltaTime;
if(directionChangeInterval < 0)
{
Push();
directionChangeInterval = DirectionChangeInterval;
}
shotInterval -= Time.deltaTime;
if(shotInterval < 0)
{
Shoot();
shotInterval = ShotInterval;
}
}
void Push()
{
float force = Random.Range(MinForce, MaxForce);
float x = Random.Range(-1f, 1f);
float y = Random.Range(-1f, 1f);
GetComponent<Rigidbody2D>().AddForce(force * new Vector2(x, y));
}
void Shoot()
{
ship = GameObject.FindGameObjectWithTag("Ship");
if (ship != null)
{
float angle = (Mathf.Atan2(ship.transform.position.y - transform.position.y, ship.transform.position.x - transform.position.x) - Mathf.PI / 2) * Mathf.Rad2Deg;
Instantiate(AlienBullet, transform.position, Quaternion.Euler(new Vector3(0f, 0f, angle)));
}
}
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.tag == "ShipBullet")
{
Instantiate(AlienExplosion, transform.position, new Quaternion());
Destroy(collider.gameObject);
Destroy(gameObject);
}
else if(collider.gameObject.tag == "Asteroid")
{
Instantiate(AlienExplosion,transform.position, new Quaternion());
Destroy(gameObject);
}
}
}
And the DodgeScript goes on the child of the Alien, along with the CircleCollider2D :
using UnityEngine; using System.Collections;
public class DodgeScript : MonoBehaviour {
public GameObject AlienShip;
public float DodgeCooldown = 0.5f;
private float Force;
private float dodgeCooldown;
// Use this for initialization
void Start ()
{
Force = AlienShip.GetComponent<AlienScript>().MaxForce;
dodgeCooldown = -1f;
}
// Update is called once per frame
void Update ()
{
dodgeCooldown -= Time.deltaTime;
}
void OnTriggerEnter2D(Collider2D collider)
{
if(dodgeCooldown < 0)
{
AlienShip.GetComponent<Rigidbody2D>().AddForce(-AlienShip.GetComponent<Rigidbody2D>().velocity.normalized * Force);
dodgeCooldown = DodgeCooldown;
}
}
}
So I think the problem is that somehow the collider on the Alien and the DodgeCollider collider are somehow both triggering as the Alien collider, and therefore both killing the alien.
Is there any way for the DodgeCollider to not kill the Alien, but instead actually have him dodge?
Sorry for the really long question, but thanks for all of your help!
Thank you!
Follow this Question
Related Questions
SImple but IMPOSSIBLE Enemies keep dying way ahead of time ! 0 Answers
Display UI Panel on 2D Box Collider Trigger 0 Answers
Problem with hitboxes and hurtboxes 0 Answers
Main Menu Help! 1 Answer
Gameobject goes missing after second iteration of event 1 Answer