MobController
So I am attempting to create a mob controller like the controller I currently have on my Player. So Basically how it is set up is if player moves on X/Y then the animation is called from the Blend Tree to play respected animation and so on. So I also have a random moving script (not what it is called) but basically it moves the mob in random directs. So I took the current Random directs part and added it to the play animations bit and here is what I came up with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MobController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D myRigidbody;
private bool moving;
public float timeBetweenMove;
private float timebetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter;
private Vector3 movedirection;
public float waitToReload;
private bool reloading;
private GameObject thePlayer;
public Vector2 lastMove;
private bool BirdMoving;
private float currentMoveSpeed;
private Animator anim;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
//timebetweenMoveCounter = timeBetweenMove;
//timeToMoveCounter = timeToMove;
timebetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);
}
// Update is called once per frame
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = movedirection;
if (timeToMoveCounter < 0f)
{
moving = false;
//timebetweenMoveCounter = timeBetweenMove;
timebetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
}
}
else
{
timebetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timebetweenMoveCounter < 0f)
{
moving = true;
// timeToMoveCounter = timeToMove;
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);
movedirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}
}
if (reloading)
{
waitToReload -= Time.deltaTime;
if (waitToReload < 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
thePlayer.SetActive(true);
}
}
if (Input.GetAxis("Horizontal") > 0.5f || Input.GetAxis("Horizontal") < -0.5f)
{
// transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
myRigidbody.velocity = new Vector2(Input.GetAxis("Horizontal") * currentMoveSpeed, myRigidbody.velocity.y);
BirdMoving = true;
lastMove = new Vector2(Input.GetAxis("Horizontal"), 0f);
}
if (Input.GetAxis("Vertical") > 0.5f || Input.GetAxis("Vertical") < -0.5f)
{
// transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxis("Vertical") * currentMoveSpeed);
BirdMoving = true;
lastMove = new Vector2(0f, Input.GetAxis("Vertical"));
}
if (Input.GetAxis("Horizontal") < 0.5f && Input.GetAxis("Horizontal") > -0.5f)
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxis("Vertical") < 0.5f && Input.GetAxis("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("BirdMoving", BirdMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
}
Problem is the Mob no longer moves and doesn't play animation at all. What am I doing wrong? Did I forget some code or use the wrong (Input.GetAxisRaw) command??? So confused. If I only use from
if (reloading)
{
waitToReload -= Time.deltaTime;
if (waitToReload < 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
thePlayer.SetActive(true);
}
}
The mob moves around and plays the basic animation of the controller but I would like it to be able to move in different directs and play different animations.
Help please and Thank you.
Sorry if I use from
if (reloading)
{
waitToReload -= Time.deltaTime;
if (waitToReload < 0)
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
thePlayer.SetActive(true);
}
}
And Up then it works perfectly just only have the simple one animation.
Answer by Mouthbreather · Apr 12, 2017 at 03:17 AM
Okay so adding
void Start()
{
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
//timebetweenMoveCounter = timeBetweenMove;
//timeToMoveCounter = timeToMove;
timebetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);
}
Gives it the move animations however now it is only moving when the player is moving and it is not moving around the map it is just switching positions in one spot and playing respective animation...