Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dmcmaniaz · May 11, 2015 at 12:28 PM · scripting problem

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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");
 
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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()

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dmcmaniaz · May 11, 2015 at 01:36 PM 0
Share

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.

avatar image dmcmaniaz · May 11, 2015 at 01:45 PM 0
Share

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;
             }
     
     
 }

alt text

animator.png (81.6 kB)
animator2.png (169.7 kB)
avatar image barbe63 · May 11, 2015 at 01:58 PM 0
Share

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;
  }

 

avatar image dmcmaniaz · May 11, 2015 at 02:18 PM 0
Share

it didn't work..$$anonymous$$d you to explain a bit for me please

avatar image barbe63 · May 11, 2015 at 02:43 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dmcmaniaz · May 11, 2015 at 02:52 PM 0
Share

Still not working. The Infinite running sequence won't stop when i pressed Shiftkey. I'm really confuse...

avatar image barbe63 · May 11, 2015 at 03:09 PM 0
Share

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.

avatar image zviaz · May 11, 2015 at 03:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges