- Home /
how to make your character run when you hold shift and w key?
Hello everyone. I'm still new in programming and Unity. Currently, i am developing game look like Resident Evil Remastered HD that release recently. I use Maximo's Basic locomotion script and animator for my character, which does not include run animation. I really want my game have the similar control scheme like Resident Evil classic.
My question is how to modify the Maximo script and add the shift key as a trigger for running? Please help me.
Answer by jamesbice1 · Jan 29, 2017 at 09:48 AM
This is a script I use and made for the movement of my player. It allows me to walk up and down, turn, and run.
using UnityEngine;
public class Movement : MonoBehaviour
{
public float Speed;
public float RunSpeed;
public float NormalSpeed;
public bool isRunning = false;
void Update() {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * Speed;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) {
isRunning = true;
Speed = RunSpeed;
print ("Running");
} else {
isRunning = false;
Speed = NormalSpeed;
print ("Not Running");
}
}
}
Answer by barbe63 · May 11, 2015 at 01:13 PM
You should first identify the part of the script that is launching the walk animation and then if you don't figure how it works try to learn that before going further :p Scripting is the key to any project you'll have so go learn first. hint: it must be an event in OnGUI()
i manage to get my character start the run animation now but i can't stop it now...i just tap the shift button once to make the character but i want to hold the shift button. Shall i upload my source codes and animator? maybe you can see it what's wrong with my coding.
here's my source code: using UnityEngine; using System.Collections;
public class LocomotionScript : $$anonymous$$onoBehaviour { private Animator anim; public float speed = 5f; public float speedRunning = 7.5f;
// Use this for initialization
void Start () {
anim = this.transform.GetComponent<Animator>();
}
void OnGUI () {
GUILayout.Label("CONTROLS");
GUILayout.Label("$$anonymous$$ovement: W A S D");
GUILayout.Label("Jump: Spacebar");
}
// Update is called once per frame
void Update () {
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
//anim.SetFloat ("Speed", vertical);
//anim.SetFloat ("Direction", horizontal);
anim.SetFloat("Vertical", vertical, 0.15f, Time.deltaTime);
anim.SetFloat("Horizontal", horizontal, 0.15f, Time.deltaTime);
//Procedural rotation input for stationary turning
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){
anim.SetFloat("Turn", -1, 0.1f, Time.deltaTime);
this.transform.Rotate(Vector3.up * (Time.deltaTime + -2), Space.World);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
anim.SetFloat("Turn", 1, 0.1f, Time.deltaTime);
this.transform.Rotate(Vector3.up * (Time.deltaTime + 2), Space.World);
}
else { anim.SetFloat("Turn", 0, 0.1f, Time.deltaTime); }
//Pressing the space bar will cause the character to jump
if (Input.GetButton("Jump")){
StartCoroutine(TriggerAnimatorBool("Jump"));
}
/* Player Run */
bool shiftHold = Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift);
bool moved = false;
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift)) {//up
anim.SetFloat("speed", speed, 1.00f, Time.deltaTime);
moved = true;
}
}

You could maybe do with this to check if shift is pressed while walking
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift))
{
// run forward animation
isRunning=true;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift))
{
// run backward animation
isRunning=true;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && isRunning)
{
//stop running animation
isRunning=false;
}
it didn't work..$$anonymous$$d you to explain a bit for me please
You have to put the anim.setTrigger, anim.SetBool or anim.SetFloat functions ins$$anonymous$$d of my commented lines
With your example running forward could be:
anim.SetFloat("speed", speed, 1.00f, Time.deltaTime);
and stop running could be:
anim.SetFloat("speed", 0);
But as i'm not aware of excalty how is set your animator (i don't see the running in your blend tree btw) I may be wrong. You may try this with booleans ins$$anonymous$$d of float before going in harder jobs. So that would be:
anim.SetBool("isRunning", true);
// to run
anim.SetBool("isRunning", false);
// to stop run
and make a new boolean in your animator named isRunning and assign it in your transition for run ins$$anonymous$$d of the float.
Answer by zviaz · May 11, 2015 at 02:03 PM
You want your character to run yet do not seem to have set up any parameters to tell the engine when to switch the running animation on/off.
For a start the Unity Standard Assets comes with a FPS controller (and script) that provides the functions you are looking for (running when shift is held down) which you could dissect to fine the code you need.
You appear to have a "moved" parameter (is this your running parameter?) you should use better names like IsWalking, IsIdle, IsRunning.
Either way you need a parameter that deals with running (I suggest IsRunning)
void Awake(){
public bool IsRunning = false;
anim.SetBool ("IsRunning", IsRunning);
}
void Running(){
IsRunning = true;
anim.SetBool ("IsRunning", IsRunning);
}
void StopRunning(){
IsRunning = false;
anim.SetBool ("IsRunning", IsRunning);
}
Call the Running() function if the user is pressing W and Shift and StopRunning() when they let go of the shift key.
There are multiple ways to get the same result so it depends how you want to handle the process.
Still not working. The Infinite running sequence won't stop when i pressed Shiftkey. I'm really confuse...
I think you REALLY need to do with the basics before anything else. Try to watch all the unity tutorials here : https://unity3d.com/learn/tutorials/modules/beginner/scripting
and here: https://unity3d.com/learn/tutorials/modules/beginner/animation
You can't really go without knowing most of the stuff here.
Your main problem is you have also to detect if the shift key is not pressed but you can't just do that, you need to assign it to the animator and more importantly:
You need to know what you're doing!!
That's why i gave you the advice to learn before try.
You need to use parameters to check if the shift button is being held down. If so, make the player run, if the shift key is not held down switch back to a walking animation or something. Parameters are used to control when the animator switches between animations. Typically you have two paramaters. Example; Idle > Walk is activated if the parameter IsWalking is set as true. Walk > Idle is activated if the parameter IsWalking is set to false.
I would guess you are doing something wrong with the animator parameters.
Also animator parameters are used in conjunction with a script. If a character has an IsWalking parameter then this needs to be referenced and set (IsWalking = true/false) in the script itself.
void Awake(){
bool IsWalking = false;
anim.SetBool ("IsWalking", IsWalking);
}
Sadly this can be a complicated topic if you are 100% new to animation in Unity so you really need to do some research in the Unity Documentation.
Your answer
Follow this Question
Related Questions
Placing brackets in a script 2 Answers
Make a sprite have the same color as the main light of the scene 1 Answer
Why do changes to a mesh persist until I restart Unity 1 Answer
playerprefs spawning errors. 1 Answer
Use vim with unity? 1 Answer