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 nasby321 · Sep 02, 2013 at 10:26 PM · 2dmovementaiaxissidescroll

Help with 2D AI scripting

I want to make my enemy move towards my character in the x axis, but he moves on the z axis instead. I'm not too good with scripting so here is my script and tell me what am I doing wrong.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAi : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public int maxDistance;
     
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
     }    
     
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
         
         maxDistance = 2;
     }        
         
 
     
     
     // Update is called once per frame
     void FixedUpdate () {
         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
         
         //Look at target
         
         
         //Move towards target
         myTransform.position -= myTransform.forward * moveSpeed * Time.deltaTime;
     }
     
     }
     
     
 
Comment
Add comment · Show 3
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 Hyperion · Sep 02, 2013 at 10:30 PM 0
Share

Do not use myTransform.forward. That's using the z-axis. Use myTransform.right. That's the x-axis.

avatar image DESTRUKTORR · Sep 02, 2013 at 10:31 PM 0
Share

Change myTransform.forward to myTransform.right

avatar image nasby321 · Sep 03, 2013 at 12:19 AM 0
Share

Now my problem is that the enemy keeps moving right and doesn't follow my player.

2 Replies

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

Answer by AlucardJay · Sep 02, 2013 at 10:31 PM

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html

forward : Shorthand for writing Vector3(0, 0, 1). Z-Axis.

Check what axis you are working with.

right : Shorthand for writing Vector3(1, 0, 0).

 myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // will move in relation to the right of the transform
 myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // will move left in relation to the transform
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 nasby321 · Sep 03, 2013 at 12:14 AM 0
Share

Thank you so much it worked! I'm still a beginner at coding. $$anonymous$$e I would rather do the animating and character creation.

avatar image nasby321 · Sep 03, 2013 at 12:19 AM 0
Share

Now my problem is that the enemy keeps moving right and doesn't follow my player.

avatar image
0

Answer by tBureck · Sep 02, 2013 at 10:38 PM

Hi,

instead of multiplying your myTransform.position with myTransform.forward, you should multiply it with myTransform.right to go along the red axis (X). The forward member will always have the same direction as the blue axis (Z).

Read more about the Transform class here.

Best regards

Comment
Add comment · Show 4 · 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 nasby321 · Sep 03, 2013 at 12:19 AM 0
Share

Now my problem is that the enemy keeps moving right and doesn't follow my player.

avatar image tBureck · Sep 03, 2013 at 09:19 AM 0
Share

You should then update the position depending on the position of the player object, who's transform you have already saved:


if (target.position.x < myTransform.position.x) myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
else if (target.position.x > myTransform.position.x) myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right

After this piece of code, you can then check what should happen when the enemy hits the player.

avatar image nasby321 · Sep 03, 2013 at 02:27 PM 0
Share

Thank you that worked great!

avatar image nasby321 · Sep 03, 2013 at 02:28 PM 0
Share

Now the new problem is I want him to stop following me after I kill him or for him to stop once sprite animation starts playing. Here is what I have now:

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
 
 public class EnemyAi : $$anonymous$$onoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public int maxDistance;
     private Sprite sprite;
     
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
         sprite = (Sprite)GetComponent(typeof(Sprite));
     }    
     
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
         
         maxDistance = 8;
     }        
         
 
     
     
     // Update is called once per frame
     void FixedUpdate () {
         if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
     
             myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;{
                 
             if (target.position.x < myTransform.position.x) myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
 
                     else if (target.position.x > myTransform.position.x) myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right
                 sprite.Play ("EnemyWalk");{
                 }
                 
                     if(sprite.IsAnimationPlaying ("death")){
                     
                 
             }
                 
                 
             
             
 }
             
     }
 }
     
 }

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 AI patrol not working 1 Answer

Restricting the movment in the Zaxis 2 Answers

moving AI up/down if lower/higher in Sector as player 0 Answers

How to make basic AI in a 2d game? 4 Answers

aquarium effect for 2d fish 2 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