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 LeftyTwoGuns · Sep 13, 2013 at 12:55 AM · inputgetaxis

How to script "GetAxisUp"?

I have a game with a fixed 2D camera angle, with a car that drives from left to right. I got the car moving with wheel colliders and basic motor script but for the purposes of gameplay I'd like the car to automatically brake whenever the player lets off the gas (A or D keys), A.K.A GetAxis "Horizontal"

I figure the best way to do this is a simple boolean of if the gas is let up, the car is braked and if the car is braked, apply brakeTorque (does this sound right?). But I don't know how to specify something like Input.GetAxisUp ("Horizontal")

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 getyour411 · Sep 13, 2013 at 01:59 AM

You can basically wrap that in an IF:

if(!Input.GetAxis("Horizontal")) { // do brake stuff }

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 LeftyTwoGuns · Sep 13, 2013 at 09:33 PM 0
Share

That seems the best solution but when I try to apply it to my code I get an error when trying to apply the brakeTorque. "The '!' operator cannot be applied to operand of type 'float'"

 void Update () {
         
         EngineRP$$anonymous$$ = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
         ShiftGears();
         
         audio.pitch = $$anonymous$$athf.Abs(EngineRP$$anonymous$$ / $$anonymous$$axEngineRP$$anonymous$$) + 1.0f;
         if(audio.pitch > 2.0f) {
             audio.pitch = 2.0f;
         }
         
         FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Vertical");
         FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Vertical");
         
         FrontLeftWheel.steerAngle = 10 * Input.GetAxis ("Horizontal");
         FrontRightWheel.steerAngle = 10 * Input.GetAxis ("Horizontal");
         
         if(!Input.GetAxis ("Vertical")){
             WheelCollider.brakeTorque = $$anonymous$$athf.Infinity;
         }
     
     }
avatar image getyour411 · Sep 13, 2013 at 10:11 PM 0
Share

O right, I think I've seen that before too, try changing it to

if(!Input.GetAxis("Horizontal") =0)

essentially the same thing - if the button is pressed the axis will not be zero

avatar image ArkaneX · Sep 13, 2013 at 10:22 PM 0
Share

You need == ins$$anonymous$$d of =

avatar image
0

Answer by Eric5h5 · Sep 13, 2013 at 10:19 PM

I don't think you would want a GetAxisUp function, because that would mean applying the brakes once only, when presumably you want them applied continuously as long as the horizontal axis is not used. So:

 if (GetAxis("Horizontal") == 0.0) {
     // do brake stuff
 }
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 LeftyTwoGuns · Sep 13, 2013 at 10:39 PM 0
Share

Now I got void Update () {

         EngineRP$$anonymous$$ = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
         ShiftGears();
         
         audio.pitch = $$anonymous$$athf.Abs(EngineRP$$anonymous$$ / $$anonymous$$axEngineRP$$anonymous$$) + 1.0f;
         if(audio.pitch > 2.0f) {
             audio.pitch = 2.0f;
         }
         
         FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
         FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
     
         
         if(Input.GetAxis ("Horizontal") == 0){
             FrontLeftWheel.brakeTorque = $$anonymous$$athf.Infinity;
             FrontRightWheel.brakeTorque = $$anonymous$$athf.Infinity;
         }
     
     }

No errors, but the car won't move at all. Is $$anonymous$$athf.Infinity not the way to go here? I don't want the car to move at all unless an input is pushed, I figured infinite brakeTorque would do that.

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

17 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

Related Questions

[SOLVED] Input.GetAxis("Horizontal") returns wrong value? -1 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Achieve framerate independent AddTorque rotaion with mouse drag. 2 Answers

Get axis literal keyboard or mouse value... not possible? 0 Answers

How do I use an Xbox controller axis other than the left thumbstick? 0 Answers


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