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 20, 2015 at 09:05 PM · playerenemyaxisfollow

unity 2d C # script follows the enemy player.

hello.

I have this script for development in 3D. the enemy follows the player, but it does not work well for a 2d game. How do you change in 2d so that the enemy follows the player, using the x-axis?

script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyInseguePlayer : MonoBehaviour {
 
     public Transform player;
     public float playerDistance;
     public float rotationSpeed;
     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 < 12f)
         {
             chase ();
         }
     
     }
 
     void LookAtPlayer()
     {
         Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
     
     }
 
 
     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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DanGoldstein91 · May 21, 2015 at 06:30 AM

I recently coded an enemy to only chase the player on an X axis in Unity2D. My object has a rigid body, so I can get away with using this:

 void chase() {
     transform.position = Vector2.MoveTowards ( transform.position.x, player.position.x, moveSpeed * Time.deltaTime );
 }

If it has to be restrained to the X-axis with code, try making a new Vector2 to specify the position, then plug that into the MoveTowards. Make sure your playerXAxis is being updated frequently. Just tested this and it works for me.

 Vector2 playerXAxis;
 playerXAxis = new vector 2 ( player.position.x, transform.position.y )
 
 void chase() {
 transform.position = Vector2.MoveTowards ( transform.position, playerXAxis );
 }

Comment
Add comment · Show 2 · 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 10:50 AM 0
Share

Thank you for your reply.

I show you how I changed the scrip with your help.

I may have something wrong? I 3 errors

script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyInseguePlayer : $$anonymous$$onoBehaviour {
 
     public Transform player;
     public float playerDistance;
     public Vector2 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 < 12f)
         {
             chase ();
         }
     
     }
 
     void LookAtPlayer()
     {
         Vector2 playerXAxis;
         playerXAxis = new Vector2 ( player.position.x, transform.y );
     }
 
 
     void chase()
     {
           
         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
         }
     }
 
avatar image DanGoldstein91 · May 23, 2015 at 09:20 AM 0
Share

Hi, sorry.

   playerXAxis = new Vector2 ( player.position.x, transform.y );

This should be transform.position.y. If you have any more errors please paste them in and let me know which lines the errors are on.

avatar image
0

Answer by Awss · Sep 07, 2017 at 01:50 PM

I have found this code works, based on this video tutorial link text

 using UnityEngine;
 using System.Collections;
 
 public class FollowAI : MonoBehaviour
 {
     public GameObject target; //the enemy's target
     public float moveSpeed = 5; //move speed
     void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player");
     }
     void Update()
     {
 
         Vector3 targetDir = target.transform.position - transform.position;
         float angle = Mathf.Atan2(targetDir.y,targetDir.x) * Mathf.Rad2Deg - 90f;
         Quaternion q = Quaternion.AngleAxis(angle,Vector3.forward);
         transform.rotation = Quaternion.RotateTowards(transform.rotation,q,180);
         transform.Translate(Vector3.up * Time.deltaTime * moveSpeed);
     }
 }


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 unknownuser2010 · Jul 30, 2020 at 10:09 AM 0
Share

I would like to inform you that this script works although when i drag in the player to the component or script and click play, the gameobject i dragged in disappears, i have to drag in the gameobject again, i'm not sure what the problem is but the script seems to work.

avatar image
0

Answer by Lacheck · Apr 18, 2019 at 11:36 AM

If your view is not from above, but from side I suggest adding:

if(Vector3.Dot(transform.up, Vector3.down) > 0) { transform.Rotate(180f, 0f, 0f); }

Comment
Add comment · 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

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

23 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

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