Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Position 2D and colisions

Discussion in 'Scripting' started by 787gabriel777, Dec 16, 2015.

  1. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Hello, i have these scripts
    Code (JavaScript):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemey02 : MonoBehaviour
    5. {
    6.  
    7.     public Transform target;//set target from inspector instead of looking in Update
    8.     public float speed = 3f;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.  
    19.         //rotate to look at the player
    20.         transform.LookAt(target.position);
    21.         transform.Rotate(new Vector2(0, -90), Space.Self);//correcting the original rotation
    22.  
    23.  
    24.         //move towards the player
    25.         if (Vector2.Distance(transform.position, target.position) > 1f)
    26.         {//move if distance from target is greater than 1
    27.             transform.Translate(new Vector2(speed * Time.deltaTime, 0));
    28.         }
    29.  
    30.     }
    31.  
    32. }
    and
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lifeIA:int;
    4. var diff : int;
    5. function Start () {
    6.     diff = 1;
    7.     lifeIA = 100 + (25*diff);
    8. }
    9. function Update () {
    10.  
    11. }
    12.     function OnCollisionEnter(collision: Collision) {
    13.             if (collision.gameObject.tag == "Dano01")
    14.             {
    15.                 lifeIA = lifeIA - 100;
    16.                 print ("Morre");
    17.             }
    18.             if (collision.gameObject.tag == "Dano02")
    19.             {
    20.                 lifeIA = lifeIA - 15;
    21.             }
    22.             if (collision.gameObject.tag == "Dano03")
    23.             {
    24.                 lifeIA = lifeIA - 20;
    25.             }
    26.  
    27.  
    28. }
    I DONT SNOW WHAT IS WRONG, BUT, WHEN MY BULLET COLIDE WITH MY IA, THE IA CONTINUE WITH THE NORMAL LIFE
    !
     
  2. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    MY tag IS OK, what is wrong?
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If this is all 2d you should be using collision2d & trigger2d stuff. Note that there is a known bug with these in the current versions & a fix is coming.
     
  4. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    But, my IA dont die, he just colide, what i can do?
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You need to define what you want to happen when the life value is <=0 using another if statement checking the value of lifeIA. At the moment you are just telling it to reduce the value.
     
  6. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
  7. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    But i have another problem, when i colide my bullet with my IA, my script dont destroy the bullet, what is wrong?
    AND
    The enemy dont destroy!
    This is my script:
    Code (JavaScript):
    1. #pragma strict
    2. var lifeIA:int;
    3. var diff : int;
    4. function Start () {
    5.     diff = 1;
    6.     lifeIA = 100 + (25*diff);
    7. }
    8. function Update () {
    9.     if (lifeIA <= 0){
    10.         Destroy (gameObject);
    11.     }
    12. }
    13. function OnCollisionEnter(collision: Collision) {
    14.     if (collision.gameObject.tag == "Dano01")
    15.     {
    16.         lifeIA = lifeIA - 100;
    17.         print ("Morre");
    18.     }
    19.     if (collision.gameObject.tag == "Dano02")
    20.     {
    21.         lifeIA = lifeIA - 15;
    22.     }
    23.     if (collision.gameObject.tag == "Dano03")
    24.     {
    25.         lifeIA = lifeIA - 20;
    26.     }
    27.  
    THIS IS A 2D GAME
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
     
  9. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Ok, i will need to use OnTriggerEnter to my bullet and my ai. Thanks
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570

    It has to be OnTriggerEnter2D or OnCollisionEnter2D. When using 2d you need to use the correct items.
     
  11. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Dont work only that, can i do more changes, what changes i can do?
     
  12. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    As I said previously, there is a known issue with 2d collisions & triggers. A fix will be released soon by Unity.