How do I make my enemy have animations for directions during movemvent

Updated on September 11, 2018 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on August 30, 2018

I’m new to programming and still learning C# as I go. So, I was following a youtube series on making a 2D RPG top down-ish game. The player has movement in every direction with direction specific animations, as I want. As it got to making the enemy, the YouTuber’s enemy doesn’t have any direction specific animations and that’s now how my enemy is. Can I get help being lead in the right direction here? I have animations going up, down, left, and right. I have my animations made but need help with the script here. Also if someone can help adjust it for me so that the enemy only moves up down left and right instead of diagonally that would be great and much appreciated! (So over all, I want the enemy to be like the player with animations, but randomized movement and no player control) Here’s the current script.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimalMovement : MonoBehaviour {
 private Animator anim;
 public float moveSpeed;
 private Rigidbody2D myRigidbody;
 private bool moving;
 public float timeBetweenMove;
 private float timeBetweenMoveCounter;
 public float timeToMove;
 private float timeToMoveCounter;
 private Vector3 moveDirection;
 // Use this for initialization
 void Start () {
  anim = GetComponent<Animator> ();
  myRigidbody = GetComponent<Rigidbody2D> ();
  timeBetweenMoveCounter = timeBetweenMove;
  timeToMoveCounter = timeToMove;
 }
 
 // Update is called once per frame
 void Update () {
  if (moving)
  {
   timeToMoveCounter -= Time.deltaTime;
   myRigidbody.velocity = moveDirection;
   if (timeToMoveCounter < 0f)
   {
    moving = false;
    timeBetweenMoveCounter = timeBetweenMove;
   }
  }
  else
  {
   timeBetweenMoveCounter -= Time.deltaTime;
   myRigidbody.velocity = Vector2.zero;
   if (timeBetweenMoveCounter < 0f)
   {
    moving = true;
    timeToMoveCounter = timeToMove;
    moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
   }
  }
 }

 

 

  • Liked by
Reply
0 on September 11, 2018

Need more information, which direction exactly do you want the enemy to look at when moving?

  • Liked by
Reply
Cancel