- Home /
2D Top Down 4-directional RPG: Enemy Animation / Follow (beginner)
Hey, I'm very new to Unity and C# and I've been watching youtube videos with the goal to create a Zelda type videogame. I wanted to get enemy animations down today but I feel the need to ask guidance.
This is the C# script for my enemy AI:
Rigidbody2D rbody;
Animator anim;
public Transform Player;
public float ChaseSpeed = 5f;
public float Range = 5f;
float CurrentSpeed;
void Start()
{ rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
anim.SetBool("is_walking", true);
anim.SetFloat("velocity_x", movement_vector.x);
anim.SetFloat("velocity_y", movement_vector.y);
}
else {
anim.SetBool("is_walking", false);
}
if (Vector3.Distance(transform.position, Player.position) <= Range)
{
CurrentSpeed = ChaseSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, Player.position, CurrentSpeed);
}
}
I'm assigning animations via a blend tree with multiple animations in them, identical to this video: https://youtu.be/XZDjkQ8wEd0?t=1267. I want the enemy to follow the player and change animations depending on which direction it's going towards (north,west,south,east), i used velocity_x and velocity_y as my animation parameters. I know a large error in my animator-code is that I refer to "input" here:
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
This basically makes the AI follow the player but also react to my inputs, which is not supposed to happen. So my question is, what do I replace "input" with? Is this at all going to work or do I need to change my approach completely?
I'm just trying to understand what types of options I have utilizing my code and how I can find the correct code-keywords to do what I want. I want to learn C# through unity, are there any noticable 2D Top down tutorial/books people would recommend me?
Any help is really appreciated!
I feel you bro, I'm in the same position... I don´t know how to detect the "Direction/Axis" in wich the enemy is moving. Have you solved the problem by the way??
2020 and I'm still waiting for a response for this xD
Answer by KevinW720 · May 27, 2020 at 05:51 PM
I know this is an old question but hopefully this will be useful to someone in the future.
You're correct that the solution can be fixed within the "input" part.
Instead of "Horizontal" and "Vertical", replace it with your rigidbody velocity.
Vector2 movement_vector = new Vector2(rbody.velocity.x, rb.velocity.y);
Hi Kevin ive got the exact same problem, losing my $$anonymous$$d over this please help end my suffering! public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public Animator anim;
public float speed = 10.0f;
public Rigidbody2D rb;
protected Vector2 movement;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
float InputX = Input.GetAxis("Horizontal");
float InputY = Input.GetAxis("Vertical");
//if (InputX != 0 || Input !=0)
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
anim.SetBool("walking", true);
if (InputX > 0.5f)
{
anim.SetFloat("speedX", 1f);
}
else if (InputX < -0.5f)
{
anim.SetFloat("speedX", -1f);
}
else if (InputX > 0 && InputX <= 0.5f)
{
anim.SetFloat("speedX", 0.5f);
}
else if (InputX < 0 && InputX >= -0.5f)
{
anim.SetFloat("speedX", -0.5f);
}
else
{
anim.SetFloat("speedX", 0);
}
if (InputY > 0.5f)
{
anim.SetFloat("speedY", 1f);
}
else if (InputY < -0.5f)
{
anim.SetFloat("speedY", -1f);
}
else if (InputY > 0 && InputY <= 0.5f)
{
anim.SetFloat("speedY", 0.5f);
}
else if (InputY < 0 && InputY >= -0.5f)
{
anim.SetFloat("speedY", -0.5f);
}
else
{
anim.SetFloat("speedY", 0);
}
}
else
{
anim.SetBool("walking", false);
}
}