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

Hostile AI Scripting

Discussion in 'Scripting' started by Lipptard, May 13, 2020.

  1. Lipptard

    Lipptard

    Joined:
    May 6, 2020
    Posts:
    3
    Hey, so, I'm a COMPLETE beginner with mediocre-at-best experience in scripting. I'm coding an enemy for a project of mine, with the intention of making him run towards the player and then instantly kill them upon contact.

    He seems to be able to walk towards the player just fine, the only problems are that he moves slowly as hell and you have to be right in his face for him to even walk towards you. Is there any way I can make him move faster and expand the range in which he notices the player.

    Here's the code I'm using:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class chase : MonoBehaviour {
    6.    public Transform player;
    7.    static Animator anim;
    8.  
    9.    void Start ()
    10.    {
    11.        anim = GetComponent<Animator>();
    12.    }
    13. void Update ()    
    14. {
    15.     if(Vector3.Distance(player.position, this.transform.position) < 10)
    16.     {
    17.         Vector3 direction = player.position - this.transform.position;
    18.         direction.y = 0;
    19.  
    20.         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
    21.        
    22.         anim.SetBool("isIdle",false);
    23.         if(direction.magnitude < 1)
    24.         {
    25.             this.transform.Translate(0,0,0.1f);
    26.             anim.SetBool("isWalking",true);
    27.             anim.SetBool("isAttacking",false);
    28.         }
    29.         else
    30.         {}
    31.             anim.SetBool("isAttacking",true);
    32.             anim.SetBool("isWalking",false);
    33.             StartCoroutine(InflictDamage());
    34.  
    35.         }
    36.         else
    37.         {
    38.        
    39.             anim.SetBool("isIdle", true);
    40.             anim.SetBool("isWalking", false);
    41.             anim.SetBool("isAttacking", false);
    42.            
    43.         }
    44.     }
    45.     IEnumerator InflictDamage ()
    46.     {
    47.         yield return new WaitForSeconds(1.1f);
    48.         GlobalHealth.currentHealth -=1;
    49.         yield return new WaitForSeconds(0.1f);
    50.     }
    51.  
    52.  
    53.  
    54.  
    55. }
    56.  
    57.  
    58.  
    Any help would be greatly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    There's a lot of magic numbers in here. I recommend replacing them with constants that are more useful.

    From just 30 seconds of inspection I see that line 15 has the value 10 in it, which I believe implies a complete do-nothing-more range... why don't you rename that 10 to be:

    Code (csharp):
    1. const float DoNothingDistance = 10.0f;
    Then on line 23 you have a magic number 1 that seems to control the distance before it walks forward... why don't you make that number be something like:

    Code (csharp):
    1. const float ApproachThePlayerDistance = 1.0f;
    In other news, a distance of 1 is the size of the default cube, perhaps a reason for your observation of "you have to be right in his face" ?

    Finally on line 25 you have a magic number called 0.1f that seems to be the walk speed. Why don't you make that a) larger so he moves faster and b) put it in a constant like:

    Code (csharp):
    1. const float WalkSpeed = 10.0f;
    Usually one would implement it by multipying it by Time.deltaTime every frame, and you can google about that.

    Finally in the inflict damage coroutine you have some other time constants. Why not give them meaningful names according to what they do? It will only make your codebase much more useful and meaningful.
     
  3. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    You might want to multiply a speed variable unless you are using (Rootmotion? CharacterController? RigidBody? NavMeshAgent?) within your translate, multiplied by Time.deltaTime. Your range check there is less than one meter btw.

    There are some other glaring syntax and flow problems here. if,else,else / numbers that do nothing.

    Unity learn premium is free for the next month or so. I'd go hit the C# survival guide course. If you just want to watch/type and hit the ground running with some basic AI, I'd recommend Holistic3D on youtube.
     
  4. Lipptard

    Lipptard

    Joined:
    May 6, 2020
    Posts:
    3
    Alright. Thanks man. I'll try it out.