• 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 Lekomese · Mar 28, 2019 at 04:04 AM · character movementgetaxisgetkeydown

how can i make my character run and walk ?

Hi, i want to make my character move, like when i hold down "D" button he walk and when i combine it with "Shift" key he start to run. The problem here is I move my character with input.GetAxis, and inside input.getAxis we can customize the buttons like we want to use, i wonder how can I combine the "Shift" key or any key with the key that i add before in the input. getAxis so that can make my character run, and when i release he back to walk.

sorry if my english is bad, i'm beginner with unity.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Saiguru · Mar 28, 2019 at 05:17 AM

Hi @Lekomese

  • For ease of use, you can use the FPS Controller from Characters by importing from Standard Assets.

  • Else you can use a "float runMultiplier" and multiply with the existing speed and if you press Shift just change the "runMultiplier" value so that it runs faster

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 RobAnthem · Mar 28, 2019 at 05:17 AM

For motion like this we normally use a "locomotion" controller, which is usually a blend tree in Animator state machine, you can simply make it so that if vertical input is greater than zero, you set anim move speed to say .5f, and then if you are holding shift, you set it to 1.0f. Then in your blend tree, blend to Run animation if speed is > .5f,, blend to walk if > 0 and < .5f, and if 0 blend to Idle anim. You can also use a matrix in blend tree and add additional parameters for strafing and such. Simply to combine keys though, you can do this.

 void Update()
 {
     float vertical = Input.GetAxis("Vertical");
     if (vertical > 0)
     {
         if (Input.GetKey(KeyCode.LeftShift))
         {
             // running
         }
         else
         {
             //walking
         }
     }
 }
Comment
Add comment · Show 1 · 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 Lekomese · Mar 29, 2019 at 05:06 AM 0
Share

Thank you sir for the answer, but I am still confused by that, I am now making a 2d platformer game and this my script code to make the character walk.

 public CharacterController2D $$anonymous$$ontrol;
 public Float WalkSpeed = 50f;
 float Horizontal2d = 0f;
   
 
  void Update()
     {
 
         Horizontal2d = Input.GetAxisRaw("Horizontal") * WalkSpeed;
 
         Animator2d.SetFloat("Walk", $$anonymous$$athf.Abs(Horizontal2d));
      }
 
  Void FIxedUpdate()
    {        
        $$anonymous$$ontrol.$$anonymous$$ove(Horizontal2d* Time.fixedDeltaTime, false, false);
         
     }


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input getkey, keyup and keydown not working?! 1 Answer

is possible mouse wheel key in else than GetAxis? 2 Answers

Button Particle Emission 1 Answer

Get numeric key 4 Answers

How does unity focus GUI when different keys are pressed? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges