I have two animations on my character:
DrawWeapon
HolsterWeapon
In the animator, they are both on a layer called “Holster”.
I have a boolean set up in mecanim that I want to use to control whether the weapon is currently holstered or not. I want the holstered boolean to be toggled between true and false upon pressing “q”.
Here is my attempted code(I excluded the unnecessary parts):
using UnityEngine;
using System.Collections;
public class BusinessmanController : MonoBehaviour {
private Animator anim; // Reference to the animator component.
private BusinessmanHashes hash; // Reference to the HashIDs.
public bool holsteredLocal = true;
void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
hash = GetComponent<BusinessmanHashes>();
}
void Update ()
{
//AudioManagement(shout);
if (Input.GetKeyDown("q")) {
Debug.Log ("go");
if (holsteredLocal) {
holsteredLocal = false;
anim.SetBool(hash.holsterBool, false);
}
if (!holsteredLocal) {
holsteredLocal = true;
anim.SetBool(hash.holsterBool, true);
}
}
}
void DrawManagement (bool holstered)
{
// If the draw input has been pressed...
if (holstered) {
gameObject.GetComponent<HeadLookControllerImproved>().enabled = false;
}
if (!holstered) {
gameObject.GetComponent<HeadLookControllerImproved>().enabled = true;
}
}
}
The script compiles okay, but it doesn’t really do anything at all when I press ‘q’. I’m new to mecanim, can anyone help?
Nevermind, solved! Just had to adjust some things:
using UnityEngine;
using System.Collections;
public class BusinessmanController : MonoBehaviour {
public float speedDampTime = 0.1f; // The damping for the speed parameter
private Animator anim; // Reference to the animator component.
private BusinessmanHashes hash; // Reference to the HashIDs.
public bool holsteredLocal = true;
void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
hash = GetComponent<BusinessmanHashes>();
// Set the weight of the layer to 1.
anim.SetLayerWeight(1, 1f);
}
void FixedUpdate ()
{
// Cache the inputs.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//bool sneak = Input.GetButton("Sneak");
MovementManagement(h, v);
}
void Update ()
{
if (Input.GetButtonDown("Draw")) {
Debug.Log ("go");
if (holsteredLocal == true) {
holsteredLocal = false;
anim.SetBool(hash.holsterBool, false);
}
else if (holsteredLocal == false) {
holsteredLocal = true;
anim.SetBool(hash.holsterBool, true);
}
}
//DrawManagement(holsteredLocal);
}
void MovementManagement (float horizontal, float vertical)
{
// Set the sneaking parameter to the sneak input.
//anim.SetBool(hash.sneakingBool, sneaking);
// If there is some axis input...
if(horizontal != 0f || vertical != 0f)
{
// ... set the players rotation and set the speed parameter to 5.5f.
////Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
// Otherwise set the speed parameter to 0.
anim.SetFloat(hash.speedFloat, 0);
}
void DrawManagement (bool holstered)
{
// If the draw input has been pressed...
if (holstered) {
gameObject.GetComponent<HeadLookControllerImproved>().enabled = false;
}
if (!holstered) {
gameObject.GetComponent<HeadLookControllerImproved>().enabled = true;
}
}
// AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
//}
}