- Home /
2d Top Down Enemy Control (LoZ type)
So I'm in the process of testing out certain ideas in Unity since the latest 2D update. I've always been obsessed with the play style of Zelda games and wanted to create something along that play style.
I've pretty much got the basics covered at this point thanks to plenty of tutorials I've been able to find across the web. However, I've been unable to get what I've learned and apply it to Enemy Movement, at least in regards to animation. I can get the enemies to follow me no problem and change animation direction based on my characters movement, however, if I don't have the movement of my character as a factor, the enemy stays in the "forward facing" direction.
For instance, here's my playerController:
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
// The speed of the player
public Vector2 speed = new Vector2(1, 1);
// Store the movement
private Vector2 movement;
// Animation purposes
private Animator animator;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// Retrieve axis informatio
var vertical = Input.GetAxis("Vertical");
var horizontal = Input.GetAxis("Horizontal");
// Movement per direction
movement = new Vector2(speed.x * horizontal, speed.y * vertical);
if (vertical > 0)
{
animator.SetInteger("Direction", 2);
animator.SetFloat("Speed", 1.0f);
}
else if (vertical < 0)
{
animator.SetInteger("Direction", 0);
animator.SetFloat("Speed", 1.0f);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 1);
animator.SetFloat("Speed", 1.0f);
}
else if (horizontal > 0)
{
animator.SetInteger("Direction", 3);
animator.SetFloat("Speed", 1.0f);
}
else
{
animator.SetFloat("Speed", 0.0f);
}
// Attacking
bool attack = Input.GetButtonDown("Fire1");
attack |= Input.GetButtonDown("Fire2");
// Careful: For Mac users, ctrl + arrow is a bad idea
if (attack)
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
// false because the player is not an enemy
weapon.Attack(false);
}
}
}
void FixedUpdate()
{
// Move the game object
rigidbody2D.velocity = movement;
}
}
And here's my Enemy "AI" movement script:
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
private Animator animator;
private Vector3 Enemy;
private Vector2 EnemyDirection;
private int Ground;
public float xDir;
public float yDir;
private float speed = 110;
void Start ()
{
animator = this.GetComponent<Animator>();
Ground = 1 << 12;
}
void Update ()
{
Enemy = GameObject.Find ("Player").transform.position;
xDir = Enemy.x - transform.position.x;
yDir = Enemy.y - transform.position.y;
EnemyDirection = new Vector2 (xDir, yDir);
//line of sight
if (!Physics2D.Raycast(transform.position, EnemyDirection, 1, Ground))
{
rigidbody2D.AddForce(EnemyDirection.normalized * speed);
}
// directional control
if (yDir > 1)
{
animator.SetInteger("Direction", 2);
animator.SetFloat("Speed", 1.0f);
}
else if (yDir < -1)
{
animator.SetInteger("Direction", 0);
animator.SetFloat("Speed", 1.0f);
}
else if (xDir < -1)
{
animator.SetInteger("Direction", 1);
animator.SetFloat("Speed", 1.0f);
}
else if (xDir > 1)
{
animator.SetInteger("Direction", 3);
animator.SetFloat("Speed", 1.0f);
}
else
{
animator.SetFloat("Speed", 0.0f);
}
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log ("Hit " + other.gameObject);
}
}
Just a heads up, I'm sort of a noob when it comes to C#, and had more experience with JavaScript and C++, however, I figure now is the perfect time to learn something new right?
Try setting the forward vector of its transform to the direction you're moving in
transform.forward = EnemyDirection.normalized;
D'oh my apologies, things got hectic and I forgot how to find this (really new to the forums).
I made some slight changes to the code to further match the player character movement before I got a chance to read your suggestion:
using UnityEngine;
using System.Collections;
public class AI : $$anonymous$$onoBehaviour {
// Animation purposes
private Animator animator;
private Vector3 Enemy;
private Vector2 EnemyDirection;
private int Ground;
// The speed of the enemy
public Vector2 speed = new Vector2(1, 1);
// Store the movement
private Vector2 movement;
// Use this for initialization
void Start ()
{
animator = this.GetComponent<Animator>();
Ground = 1 << 12;
}
void Update ()
{
Enemy = GameObject.Find ("Player").transform.position;
// Retrieve axis information
var horizontal = Enemy.x - transform.position.x;
var vertical = Enemy.y - transform.position.y;
EnemyDirection = new Vector2(horizontal, vertical);
// line of sight
// if (!Physics2D.Raycast (transform.position, EnemyDirection, 1, Ground))
// {
// rigidbody2D.AddForce (EnemyDirection.normalized * speed);
// }
// else
// {
// animator.SetFloat ("Speed", 0.0f);
// }
// $$anonymous$$ovement per direction
movement = new Vector2(speed.x * horizontal, speed.y * vertical);
// directional control
if (vertical > 1)
{
animator.SetInteger("Direction", 2);
animator.SetFloat("Speed", 1.0f);
}
else if (vertical < -1)
{
animator.SetInteger("Direction", 0);
animator.SetFloat("Speed", 1.0f);
}
else if (horizontal < -1)
{
animator.SetInteger("Direction", 1);
animator.SetFloat("Speed", 1.0f);
}
else if (horizontal > 1)
{
animator.SetInteger("Direction", 3);
animator.SetFloat("Speed", 1.0f);
}
else
{
animator.SetFloat("Speed", 0.0f);
}
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log ("Hit " + other.gameObject);
}
}
Now I assume that the changes you suggested are possible in this as well. Where exactly would Is stick that? I commented out the AI following as that has been the trouble spot for me right now, as I'm getting a "error CS0019: Operator *' cannot be applied to operands of type
UnityEngine.Vector2' and `UnityEngine.Vector2'"
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
2D animation diferent right and left 0 Answers
2D Animator: how to get rid the animation "blending" 0 Answers
Delete Object After Animation (2D) 6 Answers