Search Unity

Games objects are not working

Discussion in '2D' started by Tiago_9_, Oct 6, 2019.

  1. Tiago_9_

    Tiago_9_

    Joined:
    Apr 24, 2019
    Posts:
    4
    Hello. I have an enemy object that throws projectiles and there are several of them scattered around the map, it is expected that when the player is close to one of the enemies it will shoot, but the enemy that is nearby does not shoot, another that is far away from the player is who shoots, if I can help, I will be grateful.
     
  2. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    Can you share the code to see what you are doing?
     
    Tiago_9_ likes this.
  3. Tiago_9_

    Tiago_9_

    Joined:
    Apr 24, 2019
    Posts:
    4
    I used an object as a distance manager did not give, then put this checker in the Police script, did not give, did a lot and did not give.
     

    Attached Files:

  4. Tiago_9_

    Tiago_9_

    Joined:
    Apr 24, 2019
    Posts:
    4
    In this compressed file there is an example video, it's in mkv format
     

    Attached Files:

  5. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    I saw the DistanceManager and the Police script, and I don't find any error. Did you check if the distance value you assign from inspector is a low value like 1.5f?

    And for easier debugging, do this.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ShootWhenIsClose : MonoBehaviour
    4. {
    5.     [SerializeField, Tooltip("Enemy object")]
    6.     private GameObject police;
    7.     [SerializeField, Tooltip("Distance that player need to get close to shoot")]
    8.     private float distance;
    9.     [SerializeField, Tooltip("Projectile to instantiate")]
    10.     private GameObject bullet;
    11.    
    12.     // Player
    13.     private GameObject player;
    14.  
    15.     private void Start()
    16.     {
    17.         player = GameObject.FindGameObjectWithTag("Player");
    18.     }
    19.     void Update()
    20.     {
    21.         // Take distance between player and enemy
    22.         float dist = Vector2.Distance(player.transform.position, police.transform.position);
    23.  
    24.         // If 'distance' is bigger that 'dist'. Then shoot
    25.         if (distance >= dist) Shoot();
    26.     }
    27.  
    28.     void Shoot()
    29.     {
    30.         Instantiate(bullet, transform.position, Quaternion.identity);
    31.     }
    32. }
     
    Tiago_9_ likes this.
  6. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    And for Raycast I think it is not necessary to put distance. Because this will draw a line between two points (in this case, the transformation and the point you indicate), and check if the object with the layer player is inside. So, if the player is inside, this will return a Raycast with the information of the target player.
    For this you can use a Linecast, it's exactly the same. Here the link for Linecast
     
    Tiago_9_ likes this.
  7. Tiago_9_

    Tiago_9_

    Joined:
    Apr 24, 2019
    Posts:
    4
    Do you see video?
    Do you saw the video?
     
  8. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    Yes, the enemies shoot when the player is far away and stop when they are near.

    If you are using raycast in the video. You might have put the wrong points. By the way the distance that goes on the raycast, it should be the distance it gives you from your transform and crossHair. Not a distance entered