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
1
Question by karl_ · Jun 26, 2010 at 08:43 PM · jumpfixedupdategetbuttondown

Input.GetButtonDown inconsistent

I'm having trouble getting the Input.GetButtonDown to react consistently with this script. It works, but often times it will ignore mouse clicks. I can't figure out why it is acting with such inconsistency. Here is the code I'm working off:

// *Trip* Sidescroller Script // Based off the Unity FPS Walker //

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; private var moveDirection = Vector3.zero; private var grounded : boolean = false; var theCamera : Camera; var playerObj : GameObject; var playerCoordinates; var relative;

// Create variables for click jump var mousePosition; var AllowClickJump : boolean = true; var clickJump : float = 2; var JumpSideForce : float; var JumpUpForce : float; var StraightJump : float; var airJumps; var allowedAirJumps : int = 2;

function Awake() { clickJump = clickJump/10; }

function FixedUpdate() {

 if (grounded) {
     // We are grounded, so recalculate movedirection directly from axes
     moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection *= speed;
 }

 // Mouse Jump  
 if (Input.GetButtonDown("Fire1") && AllowClickJump && airJumps < allowedAirJumps) 
     {
         airJumps++;
         mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
         mousePosition = theCamera.ScreenToViewportPoint(mousePosition);

         playerCoordinates = new Vector3(playerObj.transform.localPosition.x, playerObj.transform.localPosition.y, 
             playerObj.transform.localPosition.z);
         pCoords = theCamera.ScreenToViewportPoint(playerCoordinates);

         Debug.Log("Mouse Click: " + mousePosition + "Char Pos" + pCoords);


         // Determine if the mouse click was left or right of the player
         if(mousePosition.x < .4)
         {
             mousePosition.x = -JumpSideForce;
             mousePosition.y = JumpUpForce;  
         }

         if(mousePosition.x > .6)
         {
             mousePosition.x = JumpSideForce;
             mousePosition.y = JumpUpForce;
         }

         if(mousePosition.x >.4 && mousePosition.x <.6)
         {
             mousePosition.y = StraightJump;
         }

         // Translate to moving action
         moveDirection = transform.TransformDirection(mousePosition);
         moveDirection = new Vector3(moveDirection.x, moveDirection.y, 0);

         // Apply the jump + modifier
         moveDirection *= clickJump;
     }   //End mouse jump

 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);
 var flags = controller.Move(moveDirection * Time.deltaTime);
 grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 if(grounded == true)
 {
     airJumps = 0;
 }

}

@script RequireComponent(CharacterController)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by Mike 3 · Jun 26, 2010 at 08:59 PM

You shouldn't use GetButtonDown with FixedUpdate

FixedUpdate isn't called every frame, and sometimes multiple times a frame, which means it'll screw up the GetButtonDown return

You should use Update instead for the button handling (and then process it in the next FixedUpdate if it's to do with physics)

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 karl_ · Jun 26, 2010 at 09:36 PM 0
Share

Great answer. Thanks a ton!

avatar image Tekksin · Jul 07, 2018 at 10:15 PM 0
Share

awarded 10 points. I was having this issue randomly, so I printed the inputs happening, and shockingly they were not all firing despite the variables they'd have to go through. Put my button inputs on an update function, and now nothing is missing its input!

I would've never deduced it was fixed updates fault, so thank you very much for this insight.

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

No one has followed this question yet.

Related Questions

Jumping not allways registering fix? 0 Answers

counter doesnt work in update 2 Answers

[C#] Make my 2D character jump in FixedUpdate 0 Answers

Problem with jump script 1 Answer

Jumping randomly doesn't work 2 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