- Home /
Rabit movement animation and coding problem
So bassicly i made simple bunny movement code that makes the bunny move at random time in a random direction. Problem is i added a new gameobject carrots witch spawn every 60 seconds and when they spawn i want the rabbit to go for them but the animations wont change and if i write some go to object scrip if the carrot spawns behind the bunny then the bunny will countinue making the "walk left" animation while actually moving right towards the carrot. Here is my bunny code and a picture of the game. I would really appreciate eny help
using UnityEngine;
using System.Collections;
public class BunnyScript : MonoBehaviour {
Animator BunnyAnimator;
float RandomNumber = 0;
float RandomMovementTime = 0;
int BunnyHealth = 100;
float TimeAfterDeath = 2f;
public static bool BunnyAlive = true;
bool LeftBorderHit = false;
bool RightBorderHit = false;
bool ActiveEvents = false;
// Use this for initialization
void Start () {
BunnyAnimator = GetComponent<Animator> ();
InvokeRepeating ("RandomMovement",RandomMovementTime,RandomMovementTime);
}
// Update is called once per frame
void Update () {
Debug.Log ("TimeAfterDeath" + TimeAfterDeath);
Vector2 mousePosition = Camera.main.ScreenToWorldPoint( Input.mousePosition );
bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains( mousePosition );
if (overSprite)
{
//If we've pressed down on the mouse (or touched on the iphone)
if (Input.GetButton("Fire1"))
{
//Set the position to the mouse position
this.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
0.0f);
}
}
Debug.Log (BunnyHealth);
if (BunnyHealth == 0) {
Destroy (gameObject);
BunnyAlive = false;
}
if (ActiveEvents == false) {
if (RightBorderHit == true) {
BunnyAnimator.SetBool ("Standing", false);
BunnyAnimator.SetBool ("MoveRight", false);
BunnyAnimator.SetBool ("MoveLeft", true);
transform.position -= transform.right * 1 * Time.deltaTime;
} else if (LeftBorderHit == true) {
BunnyAnimator.SetBool ("Standing", false);
BunnyAnimator.SetBool ("MoveLeft", false);
BunnyAnimator.SetBool ("MoveRight", true);
transform.position -= transform.right * -1 * Time.deltaTime;
} else if (RandomNumber == 1) {
BunnyAnimator.SetBool ("Standing", false);
BunnyAnimator.SetBool ("MoveRight", false);
BunnyAnimator.SetBool ("MoveLeft", true);
transform.position -= transform.right * 1 * Time.deltaTime;
} else if (RandomNumber == 2) {
BunnyAnimator.SetBool ("MoveRight", false);
BunnyAnimator.SetBool ("MoveLeft", false);
BunnyAnimator.SetBool ("Standing", true);
transform.position -= transform.right * 0 * Time.deltaTime;
} else if (RandomNumber == 3) {
BunnyAnimator.SetBool ("Standing", false);
BunnyAnimator.SetBool ("MoveLeft", false);
BunnyAnimator.SetBool ("MoveRight", true);
transform.position -= transform.right * -1 * Time.deltaTime;
}
if (RandomMovementTime <= 0f) {
RandomMovement ();
}
RandomMovementTime -= Time.deltaTime;
//Debug.Log (RandomMovementTime);
}
}
void RandomMovement (){
RandomNumber = Random.Range (1,4);
RandomMovementTime = Random.Range (3f, 7f);
LeftBorderHit = false;
RightBorderHit = false;
//Debug.Log (RandomNumber);
//Debug.Log (RandomMovementTime);
}
void OnCollisionEnter2D (Collision2D Collider) {
if (Collider.gameObject.tag == "BorderRight") {
RightBorderHit = true;
//Debug.Log ("right");
}
if (Collider.gameObject.tag == "BorderLeft") {
LeftBorderHit = true;
//Debug.Log ("left");
}
}
void OnParticleCollision (GameObject Particle){
if (Particle.gameObject.tag == "Rain") {
BunnyHealth -= 10;
}
}
void OnTriggerEnter2D (Collider2D Trigger) {
if (Trigger.gameObject.tag == "Carrot") {
BunnyAnimator.SetBool ("MoveRight", false);
BunnyAnimator.SetBool ("MoveLeft", false);
BunnyAnimator.SetBool ("Standing", true);
}
}
}
If the movement is working correctly there's a big chance the problem is in the Animator. Setting up transitions can be quite finicky so that might be the problem.
Since the problem is the animations, I think we'd need to see what does your animation controller look like and what the conditions for the ttransitions are. Actually before posting them, you could just pause the game when the bug happens and look at the animation controller to see which animation parameters are at what value and do they correspond to your transition conditions.