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 Cosmin Panfil · Apr 20, 2015 at 03:36 AM · positionenemy aifollow playerslowdownmove speed

How do I get the speed of the enemy slowed down?

Hello. I must inform you from the start that I am an amateur when comes to scripting.

I am trying to create an Arcade game like the one from -->> http://unity3d.com/learn/tutorials/projects/space-shooter

Right now I am trying to create the Enemy logic, so, I managed to put together a few lines that I found on the internet and the result is the script bellow.

"

using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour

{

 public Transform target;
 public float moveSpeed;

 private Transform myTransform;
 
 void Awake() {
     myTransform = transform;
 }    
 
 // Use this for initialization
 void Start () {
     GameObject go = GameObject.FindGameObjectWithTag("Player");
     
     target = go.transform;
 }        
 
 
 
 
 // Update is called once per frame
 void FixedUpdate () 
 {
     Debug.DrawLine(target.position, myTransform.position, Color.yellow);

     
     
     //Move on target's X position
     Vector3 playerPosition = new Vector3 (target.position.x, 0.0f, 0.0f);
     GetComponent<Rigidbody> ().velocity = playerPosition * moveSpeed; // PROBLEM
     myTransform.position = playerPosition * moveSpeed; // PROBLEM
 }

}

"

I have managed to get the enemy to seek for the Player's position on X axis. Now, my question is how do I get him moving in a slow motion maner so the Player should have the time to avoid intersecting him on the X axis.

I have tried two different things which I commented them with //PROBLEM in the script:

  1. GetComponent ().velocity = playerPosition * moveSpeed;

This line lets me to create some sort of a Slow Motion movement for the Enemy object. The thing is, using this code, the Enemy doesn't inherit the Players position. It doesn't stop to Player's X position. I have increased the Angular Drag value from the RigidBody component just to see if I can achieve a similar result to what I want but it doesn't. It moves the Enemy object with a certain speed until the Player stops from moving around.

  1. myTransform.position = playerPosition * moveSpeed;

I tried this as well. Using this line, the Enemy object inherited the Player's X position. The thing is, it follows the EXACT position which I don't want to. I want to give the Player the chance to avoid this object. That is why I tried to include the "moveSpeed" value which it did not work. Instead of decreasing the speed of the Enemy object, it multiplied the playerPosition with the value that I gave to "moveSpeed".

For example:

if moveSpeed = 1 => Enemy X position = Player X Position;

if moveSpeed = 0.5 => Enemy X position = Player X Positon / 2;

if moveSpeed = 2 => Enemy X position = Player X Position * 2;

This is not the result that I want.

I want the Enemy to inherit the Player's X Position but at the same time to slow down the movement of the Enemy object so the Player can avoid it.

Can someone help me with this?

PS: I will include a Y movement too but this movement will be constant so I don't belive it will be too troublesome. I already created that for other objects in the game using the tutorial that I mentioned from the start.

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
2
Best Answer

Answer by JoeStrout · Apr 20, 2015 at 05:18 PM

Cosmin, if I understand you correctly, you want the enemy to move towards the player's position in X, but at some limited speed. In that case, you want code like this:

 Vector3 pos = transform.position;
 pos.x = Mathf.MoveTowards(pos.x, target.position.x, Time.deltaTime * speed);
 transform.position = pos;

If you want to do something with Y, just insert that between the second and third lines above.

And whenever anybody recommends you use Lerp, be suspicious -- in most cases you want MoveTowards instead. :)

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 Cosmin Panfil · Apr 21, 2015 at 12:22 AM 0
Share

Hello Joe,

Thank you very much for your script. I had no idea that I was walking in the dungeon of a troll :lol:. I will remember your warning.

I have tested your script and the script provided from $$anonymous$$icro above. Both of them are very good. Yours is simple and neat, which I really like it. It does what has to be done. Even a newbie like me can understand it :lol:.

$$anonymous$$icro shared a pretty cool script too. I don't understand it at all but it does what I wanted. It has that smoothness when the enemy changes directions. It is pretty awesome.

Unfortunatly I can choose only one correct answer which is a bummer because both of you answered correctly...

Thank you again, Joe, for your advice and for the script and I wish you the best of luck!

$$anonymous$$ind regards, Cos$$anonymous$$ P.

avatar image JoeStrout · Apr 21, 2015 at 12:59 AM 1
Share

No worries, I'm glad you got unstuck. Good luck with your game!

avatar image
1

Answer by IrshadKhan_ · Apr 20, 2015 at 06:01 PM

Hi you can user this script

 public class NewBehaviourScript : MonoBehaviour {
     public Transform target;
     public float moveSpeed;
     public float realtime;
     
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
     }    
     
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         target = go.transform;
     }        
     
     
     
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         //Move on target's X position
         Vector3 playerPosition = new Vector3 (target.position.x, 0.0f, 0.0f);
         GetComponent<Rigidbody> ().velocity = playerPosition * moveSpeed; 
         myTransform.transform.Translate (Mathf.Clamp(moveSpeed*Time.deltaTime,0,realtime*Time.deltaTime),0f,0f);
 
     }
 
     void OnDrawGizmos(){
 
         Gizmos.color = Color.red;
         Gizmos.DrawLine(target.position,this.transform.position);
 
     }
 
 }

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 Cosmin Panfil · Apr 21, 2015 at 12:25 AM 0
Share

Hello $$anonymous$$icro,

Your script did the exact thing that I imagined.

Unfortunatly, I wasn't able to change the Y movement using your script. The script provided by Joe lets me to add a constant movemant to the Y axis which is cool. It is one of the key component of the Enemy AI that I want to build in.

As I said, bellow, I can choose only one answer which it is a real pain because both of you had very cool scripts.

Thank you very much for helping and sharing!

$$anonymous$$ind regards, Cos$$anonymous$$ P.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 2d enemy follow with Gameobjects,Unity 2d enemy follow with gamobjects 1 Answer

How can I make a flying object follow ground player and stay airborne? 2 Answers

Patrol enemy going back to origine after reaching a Waypoint 1 Answer

Generating enemies dynamically to be attached to enemt prefab 1 Answer

Script for Enemy AI (2D platformer) 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