I made a character controller with mecanim and I have an idlestate for idling, a locostate for walking and a runningstate for running. The transition from walking to running is a boolean called "Sprint". I want the character to run WHILE i press leftshift button on my keyboard, but now the character just keeps running when I pressed shift once. Thank you!
This is my script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Animator))]
[RequireComponent(typeof (CapsuleCollider))]
[RequireComponent(typeof (Rigidbody))]
public class ControlScript : MonoBehaviour
{
[System.NonSerialized]
public float lookWeight; // the amount to transition when using head look
public float animSpeed = 1.5f; // a public setting for overall animator animation speed
public float lookSmoother = 3f; // a smoothing setting for camera motion
private Animator anim; // a reference to the animator on the character
private AnimatorStateInfo currentBaseState; // a reference to the current state of the animator, used for base layer
private AnimatorStateInfo layer2CurrentState; // a reference to the current state of the animator, used for layer 2
private CapsuleCollider Col; // a reference to the capsule collider of the character
static int idleState = Animator.StringToHash("Base Layer.Idle");
static int locoState = Animator.StringToHash("Base Layer.Locomotion"); // these integers are references to our animator's states
static int jumpState = Animator.StringToHash("Base Layer.Jump"); // and are used to check state for various actions to occur
static int rollState = Animator.StringToHash("Base Layer.Roll");
void Start()
{
// initialising reference variables
anim = GetComponent();
Col = GetComponent();
if(anim.layerCount ==2)
anim.SetLayerWeight(1, 1);
rigidbody.mass = 60f;
rigidbody.freezeRotation = true;
Col.center = new Vector3(0, 1, 0);
Col.radius = 0.3f;
Col.height = 2f;
gameObject.tag = "Player";
}
void Update()
{
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
anim.SetFloat("Speed", v); // set our animator's float parameter 'Speed' equal to the vertical input axis
anim.SetFloat("Direction", h); // set our animator's float parameter 'Direction' equal to the horizontal input axis
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed'
anim.SetLookAtWeight(lookWeight); // set the Look At Weight - amount to use look at IK vs using the head's animation
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
if(anim.layerCount == 2)
layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1); // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
//JUMPING
//Idle to Jump
if (currentBaseState.nameHash == idleState)
{
if(Input.GetButtonDown("Jump"))
{
anim.SetBool("Jump", true);
}
}
//Loco to Jump
if (currentBaseState.nameHash == locoState)
{
if(Input.GetButtonDown("Jump"))
{
anim.SetBool("Jump", true);
}
}
//ROLLING
//Idle to Roll
if (currentBaseState.nameHash == idleState)
{
if(Input.GetKeyDown(KeyCode.LeftControl))
{
anim.SetBool("Roll", true);
}
}
//Loco to Roll
if (currentBaseState.nameHash == locoState)
{
if(Input.GetKeyDown(KeyCode.LeftControl))
{
anim.SetBool("Roll", true);
}
}
//AVOID REPEATING
//jumping
if (currentBaseState.nameHash == jumpState)
{
anim.SetBool("Jump", false);
}
//rolling
if(currentBaseState.nameHash == rollState)
{
anim.SetBool("Roll", false);
}
if (currentBaseState.nameHash == locoState)
{
if(Input.GetKey(KeyCode.LeftShift))
{
anim.SetBool("Sprint", true);
}
else
{
anim.SetBool("Sprint", false);
}
}
if(currentBaseState.nameHash == idleState)
{
if(Input.GetKey(KeyCode.LeftShift))
{
anim.SetBool ("Sprint", true);
}
else
{
anim.SetBool("Sprint", false);
}
}
}
}
↧