- Home /
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:
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.
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.
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. :)
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.
No worries, I'm glad you got unstuck. Good luck with your game!
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);
}
}
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
Follow this Question
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