Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by PlayCoins · Jan 31, 2017 at 08:30 PM · c#getbutton

Character walks left and right with axis horizontal, but when press Fire1 it should run but don't works correctly

My character should walk left and right with axis horizontal, this works perfectly. But now I want, that my character runs, when I press additionally "Fire1". This works partly too, but when I change the direction while pressing "Fire1" the character stops. Without pressing "Fire1" the direction change works.

 //If on platform
 if (onPlatform) {
    activeMoveSpeed = moveSpeed * onPlatformSpeedModifier;
 } else if(Input.GetButton("Fire1")) {
    activeMoveSpeed = moveSpeed * sprintSpeedModifier;
 } else {
    activeMoveSpeed = moveSpeed;
 }
 
 Debug.Log (Input.GetAxisRaw ("Horizontal"));
 
 //Player go left and right
 if (Input.GetAxisRaw ("Horizontal") > 0f) {
     myRigidbody.velocity = new Vector3 (activeMoveSpeed, myRigidbody.velocity.y, 0f);
     transform.localScale = new Vector3 (1f, 1f, 1f);
 } else if (Input.GetAxisRaw ("Horizontal") < 0f) {
     myRigidbody.velocity = new Vector3 (-activeMoveSpeed, myRigidbody.velocity.y, 0f);
     transform.localScale = new Vector3 (-1f, 1f, 1f);
 } else {
     myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);
 }
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
3

Answer by ivanparas · Jan 31, 2017 at 09:15 PM

You should always make the modifiers apply to the player's speed and change the value of the modifiers based on whether the player in on the platform or the mouse is held down. This will give you more consistent behavior. This makes it so the playing being on a platform or the mouse1 being down don't interfere with each other.

 if(onPlatform)
             {
                 onPlatformSpeed = 3.0f; //your platform speed value here
             }
             else
             {
                 onPlatformSpeed = 1.0f; //make it 1 so it doesn't affect the normal speed
             }
     
             if(Input.GetButton("Fire1"))
             {
                 sprintSpeed = 3.0f; //your platform speed value here
             }
             else
             {
                 sprintSpeed = 1.0f; //make it 1 so it doesn't affect the normal speed
             }
     
             activeMoveSpeed = moveSpeed * sprintSpeed * onPlatformSpeed;
 
Comment
Add comment · Show 2 · 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 PlayCoins · Feb 01, 2017 at 09:58 AM 0
Share

Thanks for the help :) I will try this and let you know if it solves my problem. Thanks :)

avatar image PlayCoins · Feb 02, 2017 at 07:38 PM 0
Share

Hmm, poorly it don't change the behavior, that the character stops, when I change the direction while I press "Fire1"

avatar image
0

Answer by MarshallN · Feb 03, 2017 at 08:42 PM

Something I notice is that you are only checking for sprint speed if the player is not on a platform - in other words, the only time your script cares about "Fire1" being pressed is when you aren't on a platform. The "else if" makes that happen. @ivanparas's answer fixes that, so if you've already fixed it, don't worry about it.

Also, you can clean up your movement a little bit by having something like

 float moveSpeed = activeMoveSpeed * Input.GetAxisRaw("Horizontal");
 myRigidBody.velocity = new Vector3( moveSpeed, myRigidBody.velocity.y, 0f); // Set movement speed based on activeMoveSpeed and the input.
 if(moveSpeed > 0f)
 {
     transform.localScale = new Vector3(1f, 1f, 1f);
 }
 else if (moveSpeed < 0f)
 {
     transform.localScale = new Vector3(-1f, 1f, 1f); // Make the character look to the left if horizontal movement is negative.
 }
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

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Check if a variable is true at the beginning of GetButton? 0 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 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