Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help making a mob avoiding shots:

Discussion in '2D' started by Persegan, Feb 12, 2015.

  1. Persegan

    Persegan

    Joined:
    Feb 12, 2015
    Posts:
    14
    Hello everyone, so I'm quite new to Unity and was trying to make an asteroids clone game using the 2d features of the program. One of the thing I wanted to implement was a mob I made trying to dodge asteroids and bullets as they were approaching it. The mob is a game object with animation, rigidbody 2D and a script that makes it change directions randomly and shoot bullets towards the player. It also has a box collider that interacts with player bullets, so that it can be destroyed. What I wanted to do was add a gamechild object with a greater circle collider, and make a script with a OnTriggerEnter2D that applied a force opposite of the one the ship was moving to before the collision. Here's the script:

    using UnityEngine;
    using System.Collections;

    public class AreaEsquivar : MonoBehaviour
    {
    public GameObject mob;
    public float cooldown;

    private float fuerza;
    private float cooldownprivate;
    // Use this for initialization
    void Start ()
    {
    cooldownprivate = -1f;
    fuerza = 40;
    }

    // Update is called once per frame
    void Update ()
    {
    cooldownprivate -= Time.deltaTime;
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
    if (cooldownprivate < 0f)
    {
    if (mob != null)
    {
    mob.rigidbody2D.AddForce (-mob.rigidbody2D.velocity.normalized * fuerza);
    cooldownprivate = cooldown;
    }
    }

    }
    }



    The first problem I encountered was that whenever something entered the child object hitbox, the ship automatically exploded as if it hit the parent object hitbox. After I learnt that Unity 4.5 implemented the feature that made child object collision events activate parent objects collision events aswell, which was different in Unity 4.3, I created a new empty gameobject and assigned both of my old gameobject, the old parent and child, as childs of this new gameobject, but it still doesn't work, even though now it doesn't explode when something enters the Dodge gameobject circle box radius. So I tried to disable the box collider from the mob child object, and its script, and implement there the dodging script I had in the dodging child gameobject with a new circle collider. And it worked, more or less. But I need two different hitboxes, one for the shots and the other one for dodging. I tried disabling the box collider and script of the mob object again, and enabling the dodge child object without adding anything to the mob object, and it doesn't work either. Could anyone help me please?




    In case it's necessary, I'll leave the mob script here:

    using UnityEngine;
    using System.Collections;

    public class MobScript : MonoBehaviour
    {
    public float FuerzaMin = 20f;
    public float FuerzaMax = 40f;
    public float TiempoCambioDireccion = 1f;
    public float IntervaloDisparos = 1f;
    public GameObject BalaMob;
    public float maxSpeed;
    public float leftBorder;
    public float rightBorder;
    public float topBorder;
    public float bottomBorder;
    public GameObject Explosion_Alien;

    private GameObject jugador;
    private float IntervaloCambioDireccion;
    private float intervaloDisparo;

    // Use this for initialization
    void Start ()
    {
    Empujar ();
    IntervaloCambioDireccion = TiempoCambioDireccion;
    intervaloDisparo = IntervaloDisparos;

    }

    // Update is called once per frame
    void Update ()
    {
    IntervaloCambioDireccion -= Time.deltaTime;
    if (IntervaloCambioDireccion < 0)
    {
    Empujar();
    IntervaloCambioDireccion = TiempoCambioDireccion;
    }
    intervaloDisparo -= Time.deltaTime;
    if (intervaloDisparo<0)
    {
    Disparar();
    intervaloDisparo = IntervaloDisparos;
    }


    }

    void FixedUpdate()
    {
    // Limite de velocidad
    if(rigidbody2D.velocity.magnitude > maxSpeed)
    {
    rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxSpeed;
    }

    //rebotar en los bordes de la pantalla
    if((transform.position.x <= leftBorder) && rigidbody2D.velocity.x < 0f){
    rigidbody2D.velocity = new Vector2(-rigidbody2D.velocity.x, rigidbody2D.velocity.y);
    }

    if((transform.position.x >= rightBorder) && rigidbody2D.velocity.x > 0){
    rigidbody2D.velocity = new Vector2(-rigidbody2D.velocity.x, rigidbody2D.velocity.y);
    }

    if((transform.position.y <= bottomBorder) && rigidbody2D.velocity.y < 0){
    rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, -rigidbody2D.velocity.y);
    }

    if((transform.position.y >= topBorder) && rigidbody2D.velocity.y > 0){
    rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, -rigidbody2D.velocity.y);
    }

    }

    void Empujar()
    {
    float fuerza = Random.Range (FuerzaMin, FuerzaMax);
    float x = Random.Range (-1f, 1f);
    float y = Random.Range (-1f, 1f);
    rigidbody2D.AddForce (fuerza * new Vector2(x, y));

    }
    void Disparar()
    {

    //encontrar posicion jugador
    jugador = UnityEngine.GameObject.FindGameObjectWithTag ("jugador");
    if (jugador != null)
    {

    //encontrar angulo jugador
    float angulo = (Mathf.Atan2 (transform.position.y - jugador.transform.position.y, transform.position.x - jugador.transform.position.x)+ Mathf.PI/2) * Mathf.Rad2Deg;

    //apuntar al jugador
    Instantiate (BalaMob, transform.position, Quaternion.Euler(new Vector3(0f, 0f, angulo)));
    }

    }
    void OnTriggerEnter2D(Collider2D collider)
    {
    if (collider.gameObject.tag == "bala_jugador" ||collider.gameObject.tag == "asteroide" )
    {
    Destroy (gameObject);
    Destroy (collider.gameObject);
    Instantiate (Explosion_Alien, transform.position, new Quaternion());
    }

    }
    }