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 /
  • Help Room /
avatar image
0
Question by Str1987 · Sep 19, 2017 at 11:45 AM · updatefixedupdategetbutton

GetButtonUp works but not always

Hello, people!

Some script for manipulation of ball. There is condition:

   if (Input.GetButtonUp ("Jump") & touchGround) 
             {
                 // Если еще не красный шар то обычный прыжок
                 if (r < 0.7f) {
                     rb.AddForce (Vector3.up * jump);
                     touchGround = false;
                     playerSoud.PlayOneShot (jumpBall, 1f);
                     ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
                 } 
                 // Если красный то высокий прыжок
                 if (r >= 0.7f) 
                 {
                     rb.AddForce (Vector3.up * bigJump);
                     touchGround = false;
                     playerSoud.PlayOneShot (bigJumpBall, 1f);    
                     ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
                 }

And problem that GetButtonUp works but not always, maybe you have some experience with this?

If ball on the ground and color.r of skin ball more or less 0.7f, ball do BigJump or Jump/ Below all code.

 public class ControlPlayer : MonoBehaviour {
 
     public float speed;
     public float jump;
     public float bigJump;
     public AudioClip pickUpCollected;
     public AudioClip jumpBall;
     public AudioClip bigJumpBall;
     public bool touchGround;
     public Material ballSkin;
 
     private Rigidbody rb; 
     private AudioSource playerSoud;
     private float r;
     private float g; 
     private float b;
 
 
     void Start () 
     {    // Присваиваем переменной rb доступ к Rigidbody
         rb = GetComponent<Rigidbody> ();
         playerSoud = GetComponent<AudioSource> ();
     }
 
     void Update () 
     {
         // Если переменная Фалсе (шар в полете)то обнуляем rb.AddForce для того что бы нельзя было управлять шариком в полёте
         if (touchGround == false) 
         {
             speed = 0;
         } 
         // Шар на земле, значит им моджно манипулировать, вар speed присваиваем 5
         if (touchGround == true)
         {
             speed = 5f;
         }
         // При нажатом левом шифте, умножаем скорость на 2 
         if (Input.GetButton ("Fire3")) 
         {
             speed *= 2;    
         }
         // Смена цвета шара до красного, что бы было понятно что шар готов в большому прыжку
         if (Input.GetButton ("Jump") & touchGround) 
         {
             // Смена цвета SkinBall
             r = ballSkin.color.r + Time.deltaTime;
             g = ballSkin.color.g + -Time.deltaTime;
             b = ballSkin.color.b + -Time.deltaTime;    
 
             if (r >= 0.78f) 
             {
                 r = 0.78f;
             }
             if (g <= 0.0f) 
             {
                 g = 0.0f;
             }
             if (b <= 0.0f) 
             {
                 b = 0.0f;
             }
             ballSkin.color = new Vector4 (r, g, b, 1.0f);
         }
     }
 
     void FixedUpdate ()  
     {    
         // Управление движения вперед назад вправо лево
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal, 0f, moveVertical);
         rb.AddForce (movement * speed); 
 
         // Управление прыжок
         if (Input.GetButtonUp ("Jump") & touchGround) 
         {
             // Если еще не красный шар то обычный прыжок
             if (r < 0.7f) {
                 rb.AddForce (Vector3.up * jump);
                 touchGround = false;
                 playerSoud.PlayOneShot (jumpBall, 1f);
                 ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
             } 
             // Если красный то высокий прыжок
             if (r >= 0.7f) 
             {
                 rb.AddForce (Vector3.up * bigJump);
                 touchGround = false;
                 playerSoud.PlayOneShot (bigJumpBall, 1f);    
                 ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
             }
         }    
     }  
 
     void OnTriggerEnter(Collider other) 
     {    // При столкновени с объектом с триггером PickUp - этот объект выключается
         if (other.gameObject.CompareTag ("PickUp")) 
         {
             other.gameObject.SetActive (false);
             playerSoud.PlayOneShot (pickUpCollected, 1f);
         }
         // При столкновени с объектом с триггером Ground - булевой переменной touchGround присваиваем true
         // Для того что бы можно было управлять ашриком на земле
         if (other.gameObject.CompareTag ("Ground"))
         {
             touchGround = true;
         } 
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

113 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to handle FixedUpdate() and Input 0 Answers

Transform.translate -> Update or FixedUpdate 1 Answer

Addforce in fixedupate 0 Answers

Only Disable One Update on a Script 1 Answer

Score, FixedUpdate and Update 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