- Home /
Animator Boolean not Changing by script C#
Hello! I have a problem when trying to change a state within my character.
using System.Collections; using UnityEngine;
public class Player : MonoBehaviour {
public Animator anim;
public Rigidbody rbody;
private bool isOnGround = true;
private bool jump = false;
private bool doubleJump = false;
//Stores the slide time
private bool slide = false;
public float slideTimer = 0f
//Sets the maximum amount of time to slide;
public float maxSlideTime = 0.01f;
private bool rest = false;
private float inputH;
private float inputV;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
PlayerInput();
}
void PlayerInput(){
if(Input.GetKeyDown("1")){
print ("1 is Pressed");
anim.Play("WAIT03", -1, 0f);
}
if(Input.GetMouseButtonDown(1) && !slide){
print ("isSliding anim bool set true");
//anim.Play("SLIDE00", -1, 0f);
anim.SetBool("isSliding", true);
rbody.useGravity = true;
slideTimer = 0f;
slide = true;
gameObject.GetComponent<BoxCollider>().enabled = false;
}
/*if(slide){
//slideTimer is given a tick rate in seconds.
slideTimer += Time.deltaTime;
//If slide timer is over max slide time then the slide with exit state
if(slideTimer > maxSlideTime){
anim.SetBool("isSliding", false);
slide = false;
gameObject.GetComponent<BoxCollider>().enabled = true;
}
}*/
//Physics in Fixed Update
void FixedUpdate()
{
if(jump == true){
jump = false;
rbody.velocity = new Vector2(0, 0);
rbody.AddForce(new Vector2(0, 80f), ForceMode.Impulse);
}
if(slide == true){
slide = false;
rbody.velocity = new Vector2(0, 0);
rbody.AddForce(new Vector2(80f, 0f), ForceMode.Acceleration);
}
if(rest == true){
transform.position = new Vector3(-50f, 0, 0);
}
}
}
I've isolated the problem to the code if you look in the area where I've disabled the code in second if statement in PlayerInput(){}. In my animator when I left click it sets the anim.SetBool("isSliding", true); to true. However I want to make it set to false when the "slideTimer" exceeds the "maxSlideTime". I think maybe I didn't set the "slideTimer" to tick every second.
When I enable the code the value does change, however it doesn't tick, it merely changes once at a value ranging from around 0.015 to 0.022. How do I get the slideTimer to tick every second correctly?
Much thanks in advance.
Answer by ShadyProductions · May 29, 2017 at 07:47 PM
In your fixed update you have the following which conflicts with your update method:
if(slide == true){
slide = false;
You probably need to add some more validation to that like
if (slide == true && slideTimer > maxSlideTime) {
slide = false;
How the hell did I not notice that! Thank you so much!
Your answer
Follow this Question
Related Questions
I need help with my animations and script 0 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
GameObject from macinim ? 0 Answers
Script over animation movement 1 Answer
First script plays animation but second script stops if from playing said animation 1 Answer