• 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
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

People who like this

0 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

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

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

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

Need help for bullet script,Script for bullet 0 Answers

Make enemy patrol without Nav Points? 0 Answers

vehicle transmission script problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges