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

why does my enemy shoot through walls?

Discussion in 'Scripting' started by AtomicCabbage33, Oct 10, 2015.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    my enemy can shoot through walls? I'm guessing I need a raycast. Help!

    Code (JavaScript):
    1. var Distance;
    2. var Target : Transform;
    3. var ShootRange = 25.0;
    4. var attackRange = 15.0;
    5. var moveSpeed = 5.0;
    6. var Damping = 6.0;
    7. var projectile : Rigidbody;
    8. var fireRate : float = 1;
    9. var FireRange = 0-100;
    10. private var nextFire : float = 0.0;
    11. function Update ()
    12. {
    13.         Distance = Vector3.Distance(Target.position, transform.position);
    14.      
    15.         if (Distance < ShootRange)
    16.         {
    17.                 GetComponent.<Renderer>().material.color = Color.yellow;
    18.                 lookAt();
    19.         }
    20.      
    21.         if (Distance > ShootRange)
    22.         {
    23.                 lookAt();
    24.              
    25.         }
    26.      
    27.         if (Distance < attackRange)
    28.         {
    29.                 GetComponent.<Renderer>().material.color = Color.red;
    30.                 attack ();
    31.         }
    32.      
    33.         if (Distance > attackRange && Time.time > nextFire )
    34.         {
    35.         nextFire = Time.time + fireRate;
    36.                 Instantiate(projectile, transform.position, transform.rotation);
    37.              
    38.         }
    39. }
    40. function lookAt ()
    41. {
    42.         var rotation = Quaternion.LookRotation(Target.position - transform.position);
    43.      
    44.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    45.                      
    46.      
    47.      
    48. }
    49. function attack ()
    50. {
    51.         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    52. }
     

    Attached Files:

  2. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    What is the attached screenshot? Is it the projectile?

    Is the problem you want to fix that the projectile is moving through the wall, or that the enemy is firing at the wall when the player is on the other side of it?

    If it's the first, have your projectile get destroyed when it collides with a wall.

    If it's the second, then a raycast can work. If the raycast hits a wall before the player, it would mean the wall is in the way. If you only want the raycast to only look for certain things, you can use layers.
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    The screenshot is the enemy shoot script and the problem is that the projectile goes through walls. Should I change the layer of the wall to something and if the projectile collides with it then destroy the game object?
     
  4. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    Yeah, if you want to make projectiles not go through something, you'll have to detect collision with that thing and then make your projectile blow up or get destroyed or whatever you want it to do. Layers are intended for raycasts and cameras, I think, so you might want to use tags, so you could either create a 'wall' tag, or if there are multiple objects you don't want it to be able to pass through, something like an 'unpassable' tag. But note that you can only have one tag per object so use what works best for your game.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    hax.
     
    Eater_Games likes this.
  6. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    thanks but I fixed the problem, the bullet was travelling too fast for the colliders to catch up