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

C# Enemy Shooting Ai Need help

Discussion in 'Scripting' started by HorizonHD, Feb 27, 2016.

  1. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    So far I have a C# script that makes a constant raycast to dectect movement, (When a player walks infront of it it shoots you)
    My problems are : It doesnt stop shooting when you leave the raycast
    and it only does damage to the player once and not multiple times


    ENEMY SHOOTING SCRIPT

    using UnityEngine;
    using System.Collections;

    public class EnemyShoot : MonoBehaviour
    {
    private Health _health;
    public Rigidbody projectile;
    public float speed = 50;
    public int Distance = 20;


    void Update()
    {
    RaycastHit hit;
    float theDistance;

    Vector3 forward = transform.TransformDirection(Vector3.forward) * Distance;
    Debug.DrawRay(transform.position, forward, Color.green);

    if (Physics.Raycast(transform.position, (forward), out hit))
    {
    Rigidbody instentiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
    instentiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    theDistance = hit.distance;
    _health = hit.collider.gameObject.GetComponent<Health>();
    _health.Damage(10);
    print(theDistance + " " + hit.collider.gameObject.name);

    }
    }
    }
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Do you really want to instantiate a projectile on every frame?
     
  3. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    Im not exactly sure, Is there a better way to do it? This is my first time making a script by myself, I kinda just messed around with stuff I seen in tutorials and it sorta worked. I want it to target the player, and shoot him until the player walks out of the raycast or the player is dead
     
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I guess you want to define a firing rate and instantiated projectiles accordingly. For that you could, for example, keep track of the time the last projectile has been fired and fire a new one only when there is enough time elapsed between two shots.
     
  5. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Try using Invoke() to call when you deal attacks, you can either use triggers to find if the player is in range or you can use some code by subtracting the positions of both and checking if they are close.
     
  6. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    I agree with the other comments, doing a raycast every frame is going to be expensive on the cpu, plus the instantiate is bad. Use Vector3.Distance to only do the cast when within a certain range. take all the code out of Update and make it an InvokeRepeating function which is called at a speed that matches the fire rate you want say every quarter second. Also make your projectile a prefab that is in a pool so you can reuse it. Another way would be to put a collider box infront of the game object that is doing the shooting which covers the area the shooter will shoot at, and only if you get a collision with a target do the raycast.
     
    RavenOfCode likes this.