• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by m3dsec · Apr 30, 2019 at 07:08 AM · scripting problemscript.scripting beginnerscript error

How do i make my enemies face the player? 2D

Hello

I'm currently working on a 2D game and trying to make my enemies face the player every time they detect the player in the range.

enemy AI

g System.Collections; using System.Collections.Generic; using UnityEngine; using Spine; using Spine.Unity;

public class EnemyBase : PhysicsObject {

 [SerializeField] enum EnemyType {Bug, Zombie};
 [SerializeField] EnemyType enemyType;
 public AudioSource audioSource;
 private AnimatorFunctions animatorFunctions;
 public float maxSpeed = 7;
 private float launch = 1;
 [SerializeField] float launchPower = 10;
 public float direction = 1;
 [SerializeField] public float changeDirectionEase = 1;
 private float directionSmooth = 1;
 [SerializeField] private int health = 3;
 [SerializeField] private GameObject deathParticles;
 public float jumpTakeOffSpeed = 7;
 public bool jump = false;
 private float playerDifference;
 public string groundType = "grass";
 public AudioClip stepSound;
 public AudioClip jumpSound;
 public AudioClip grassSound;
 public AudioClip stoneSound;
 public bool recovering;
 public float recoveryCounter;
 public float recoveryTime = 2;
 private bool followPlayer;
 private Transform target;
 public float followRange;
 [SerializeField] private LayerMask layerMask;

 [SerializeField] private GameObject graphic;
 [SerializeField] private Animator animator;
 [SerializeField] private bool jumping;
 private RaycastHit2D ground;
 [SerializeField] private Vector2 rayCastOffset;
 private RaycastHit2D rightWall;
 private RaycastHit2D leftWall;
 private RaycastHit2D rightLedge;
 private RaycastHit2D leftLedge;

 void Start(){
     audioSource = GetComponent<AudioSource>();
     animatorFunctions = GetComponent<AnimatorFunctions>();
     SetGroundType ();
     
 }

 void OnDrawGizmosSelected()
 {
     // Draw a yellow sphere at the transform's position
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere(transform.position, followRange);
 }

 protected override void ComputeVelocity()
 {
    
     Vector2 move = Vector2.zero;
     playerDifference = NewPlayer.Instance.gameObject.transform.position.x - transform.position.x;
     directionSmooth += (direction - directionSmooth) * Time.deltaTime * changeDirectionEase;
     if (!NewPlayer.Instance.frozen && !recovering) {
         move.x = 1 * directionSmooth;

         //Flip the graphic depending on the speed
         if (move.x > 0.01f) {
             if (graphic.transform.localScale.x == -1) {
                 graphic.transform.localScale = new Vector3 (1, transform.localScale.y, transform.localScale.z);
             }
         } else if (move.x < -0.01f) {
             if (graphic.transform.localScale.x == 1) {
                 graphic.transform.localScale = new Vector3 (-1, transform.localScale.y, transform.localScale.z);
             }
         }

         //Check floor type
         ground = Physics2D.Raycast (transform.position, -Vector2.up);
         Debug.DrawRay (transform.position, - Vector2.up, Color.green);
         transform.LookAt(target.transform);




         //Check if player is within range to follow

         if (enemyType == EnemyType.Zombie) {
             if ((Mathf.Abs (playerDifference) < followRange)) {
                 followPlayer = true;
             } else {
                 followPlayer = false;
             }
         }
             
         if (followPlayer) {
             if (playerDifference < 0) {
                 direction = -1;
             } else {
                 direction = 1;
                 
             }
         } else {
             //Allow enemy to instantly change direction when not following player
             directionSmooth = direction;
         }



         //Check for walls
         rightWall = Physics2D.Raycast (new Vector2 (transform.position.x + rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.right, 1f, layerMask);
         Debug.DrawRay (new Vector2 (transform.position.x, transform.position.y + rayCastOffset.y), Vector2.right, Color.yellow);

         if (rightWall.collider != null) {
             if (!followPlayer) {
                 direction = -1;
             } else {
                 Jump ();
             }

         }

         leftWall = Physics2D.Raycast (new Vector2 (transform.position.x - rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.left, 1f, layerMask);
         Debug.DrawRay (new Vector2 (transform.position.x, transform.position.y + rayCastOffset.y), Vector2.left, Color.blue);

         if (leftWall.collider != null) {
             if (!followPlayer) {
                 direction = 1;
             } else {
                 Jump ();
             }
         }

         if (!followPlayer)
         {
             target = GameObject.FindWithTag("Player").transform;
             
         }

         //Check for ledges
         if (!followPlayer) {
             rightLedge = Physics2D.Raycast (new Vector2 (transform.position.x + rayCastOffset.x, transform.position.y), Vector2.down, .5f);
             Debug.DrawRay (new Vector2 (transform.position.x + rayCastOffset.x, transform.position.y), Vector2.down, Color.blue);
             if (rightLedge.collider == null) {
                 direction = -1;
             }

             leftLedge = Physics2D.Raycast (new Vector2 (transform.position.x - rayCastOffset.x, transform.position.y), Vector2.down, .5f);
             Debug.DrawRay (new Vector2 (transform.position.x - rayCastOffset.x, transform.position.y), Vector2.down, Color.blue);

             if (leftLedge.collider == null) {
                 direction = 1;
             }
         }
     


     //Recovery after being hit
     } else if (recovering) {
         recoveryCounter += Time.deltaTime;
         move.x = launch;
         launch += (0 - launch) * Time.deltaTime;
         if (recoveryCounter >= recoveryTime) {
             recoveryCounter = 0;
             recovering = false;
         }
     }
         
     animator.SetBool ("grounded", grounded);
     animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);
     targetVelocity = move * maxSpeed;
 }

 public void SetGroundType(){
     switch (groundType) {
     case "Grass":
         stepSound = grassSound;
         break;
     case "Stone":
         stepSound = stoneSound;
         break;
     }
 }

 public void Jump(){
     if (grounded) {
         velocity.y = jumpTakeOffSpeed;
         PlayJumpSound ();
         PlayStepSound ();
     }
 }

 public void Hit(int launchDirection){
     NewPlayer.Instance.cameraEffect.Shake (100,1);
     health -= 1;
     animator.SetTrigger ("hurt");
     velocity.y = launchPower;

     launch = launchDirection*(launchPower/5);
     recoveryCounter = 0;
     recovering = true;
     if(health <= 0){
         Die();
     }
 }

 public void Die(){
     deathParticles.SetActive (true);
     deathParticles.transform.parent = transform.parent;
     Destroy (gameObject);
 }

 public void PlayStepSound(){
     audioSource.pitch = (Random.Range(0.6f, 1f));
     audioSource.PlayOneShot(stepSound);
 }

 public void PlayJumpSound(){
     audioSource.pitch = (Random.Range(0.6f, 1f));
     audioSource.PlayOneShot(jumpSound);
 }
       

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KevRev · Apr 30, 2019 at 07:20 AM 0
Share

Tldr.
Where you are detecting the player within a set distance, use transform.Lookat with a Lerp.
If you look at the FAQ page, there are some great tips on how to compose your question to get the best chance of a good response.
Good luck

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to instance for my timer and my text 0 Answers

Unity {[(2D)]} RotateAround while jumping 0 Answers

Flashlight flickering script? 0 Answers

What's wrong with my script? 2 Answers

Getting weird error message!?!?!? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges