Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by rOBY GAMES · May 21, 2015 at 12:49 PM · playerenemyfollow

C # script the enemy chases the player.

Hello everyone.

I have this script that does not work for the enemy. When the player approaches the enemy, it is not pursued. The enemy remains still in place. What could be the problem?

Script of the enemy:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyInseguePlayer : MonoBehaviour {
 
     public Transform player;
     public float playerDistance;
     public float playerXAxis;
     public float moveSpeed;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         playerDistance = Vector3.Distance (player.position, transform.position);
 
         if(playerDistance < 15f)
         {
             LookAtPlayer ();
         }
         if(playerDistance < 18f)
         {
             chase ();
         }
     
     }
 
     void LookAtPlayer()
     {
         Vector2 playerXAxis;
         playerXAxis = new Vector2(player.position.x, player.position.y);
     }
 
 
     void chase()
     {
           
         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
         }
     }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by komodor · May 21, 2015 at 01:24 PM

instead of using method LookAtPlayer (which doesn't do anything) use http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

and maybe check your moveSpeed because the chase should work if it's different from 0

 if (playerDistance < 15f)
 {
     transform.LookAt(player.transform);
 }
 if (playerDistance < 18f)
 {
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
 }
Comment
Add comment · Show 1 · Share
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 rOBY GAMES · May 21, 2015 at 01:46 PM 1
Share

thanks works. :-)

avatar image
1

Answer by chintan_shroff · May 21, 2015 at 03:33 PM

Few issues I would like to point out... Check if you have assigned the player in the editor. Second, LookAtPlayer() function will do nothing because you are not using playerXAxis variable to change the orientation of the enemy. Also I do not understand why would an enemy chase you first and then look at you. But this is upto your logic

 if(playerDistance < 15f)
      {
          LookAtPlayer ();
      }
      if(playerDistance < 18f) //imo, change this to a value below 15
      {
          chase ();
      }

Third you have declared playerXaxis as a float and then as a vector2 in our LookAtPlayer() function.

I was unable to figure out the issue with your code(i think its the distance bit) but here is another simple solution. Create 2 triggers around the enemy gameobjects as child objects and then check for OnTriggerEnter2D on the player script for each to get the desired output.

   void OnTriggerStay2D(Collider2D other)
 {
     if (other.gameObject.name  == "Chase Trigger"){ //ensure you name the triggers correctly
         enemy.Translate (Vector3.up * speed * Time.deltaTime, Space.World);}

     else if (other.gameObject.name  == "Look Trigger"){//ensure you name the triggers correctly
         //lootAtEnemy();
 }
Comment
Add comment · Show 3 · Share
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 chintan_shroff · May 21, 2015 at 03:07 PM 0
Share

couple of things I missed here

be sure to set the speed variable in the inspector else it will be 0 and there will be no translation

also, use Vector3.up if you are making a 2D game. Vector3.forward will move your gameobject in the Z axis

avatar image rOBY GAMES · May 21, 2015 at 07:46 PM 0
Share

I get an error in enemy.

error CS0117: Enemy' does not contain a definition for Translate'

avatar image chintan_shroff · May 22, 2015 at 03:29 AM 0
Share

make a public Transform variable called enemy, in your player script and make sure to add your enemy to the player script in the inspector

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Enemy backing away from player after going around corner? 1 Answer

Enemy is not stopping following the player 2 Answers

Enemy reorienting for player 1 Answer

Camera rotation around player while following. 6 Answers

Enemy to follow the Player 1 Answer


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