- Home /
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;
}
}
Do not use myTransform.forward. That's using the z-axis. Use myTransform.right. That's the x-axis.
Now my problem is that the enemy keeps moving right and doesn't follow my player.
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
Thank you so much it worked! I'm still a beginner at coding. $$anonymous$$e I would rather do the animating and character creation.
Now my problem is that the enemy keeps moving right and doesn't follow my player.
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
Now my problem is that the enemy keeps moving right and doesn't follow my player.
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.
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
Follow this Question
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