- Home /
Unity2D AI for Top Down Zelda-Type movement
I've been trying to wrap my head around enemy movement for a couple weeks now, and I'm not quite sure I understand very much at all.
I was using some code from a tutorial I found at some point. Here it is
void Update()
{
transform.LookAt(target.position);
transform.Rotate(new Vector3(0,-90,0),Space.Self);
if (Vector3.Distance(transform.position,target.position)>1f)
{
transform.Translate(new Vector3(speed * Time.deltaTime,0,0) );
}
}
This causes the enemy to chase the player down, but causes the sprite to rotate on all axis.
What I want, and can't seem to figure out, is for enemies to only have left-looking and right-looking states, and still move towards the player in all directions.
So there would be no actual rotation, just a flipping of the sprite direction.
Answer by DwaynePritchett · Apr 21, 2015 at 06:04 AM
If you're wanting left/right only. You won't want a transform.LookAt. That will use all axes. Try a simple if check, to see if the player is to the left or right of the enemy. Do this on the "Enemy Movement/AI" script. Basically, if(target.position.x < transform.position.x) mySprite = facing left; else my Sprite = facing right.
On another note, you might want to make a generic velocity by having your "monster" track his own position. Example... //Enemy Movement Script
Vector3 currentPosition, lastPosition;
Start(){
currentPosition = transform.position;
lastPosition = currentPosition;
}
Update(){
if(currentPosition.x > lastPosition.x){
//We are moving to the right
//set sprite to rightFacingSprite
}
if(currentPosition.x < lastPosition.x){
//We are moving to the left
//set sprite to leftFacingSprite
}
//Update the new positions
lastPosition = currentPosition;
currentPosition = transform.position;
}
This way, even if your monster hasn't "sensed/angroed" the "hero", he will still look back and forth as he moves along his default path. (Assuming, he has a default path.)
Hope this helps. NOTE* That code isn't meant to just work out of the box, but rather give you an idea of what needs done.
-Dwayne Pritchett
This is what I cam up with based on your input and a little research into Vector2s and it works how I pictured it, thanks!
using UnityEngine;
using System.Collections;
public class AI : $$anonymous$$onoBehaviour
{
public Transform target;//set target from inspector ins$$anonymous$$d of looking in Update
Quaternion enemyRotation;
Vector2 playerPos, enemyPos;
void Start()
{
enemyRotation = this.transform.localRotation;
}
void Update()
{
playerPos = new Vector2(target.localPosition.x,target.localPosition.y);//player position
enemyPos = new Vector2 (this.transform.localPosition.x, this.transform.localPosition.y);//enemy position
if (Vector3.Distance (transform.transform.position, target.transform.position) > 1.6)//move towards if not close by
{
transform.position = Vector2.$$anonymous$$oveTowards (enemyPos, playerPos, 2 * Time.deltaTime);
}
if (Vector3.Distance (transform.transform.position, target.transform.position) < 1.55)//move away if too close
{
transform.position = Vector2.$$anonymous$$oveTowards (enemyPos, playerPos, -1 * Time.deltaTime);
}
if (target.position.x > transform.position.x)//rotates enemy to the right if player is to the right
{
enemyRotation.x = 180;
transform.localRotation = enemyRotation;
}
if (target.position.x < transform.position.x)//rotates enemy to the left if player is to the left
{
enemyRotation.x = 0;
transform.localRotation = enemyRotation;
}
}
}
Your answer
Follow this Question
Related Questions
Top-Down Shooter Motion 2 Answers
2D rigidbody top down movement problem 3 Answers
My top-down movement is really jittery, any idea how to fix? 1 Answer
Top-down movement in 2D 1 Answer
Enemy strafing AI Top Down 0 Answers