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 MrGoldenPorkchop · Nov 14, 2019 at 01:20 AM · unity 5buttonif

OnTriggerEnter and UI button

Hi. I'm currently working on something and I need help. I'm making a football game and I'm trying to add kick button.I already wrote the script for kicking the ball but it automatically kicks it when I get near it. Can anyone change my script? I want it to be something like : when player enters trigger(ball) AND if he presses the UI button, then something happens. Is it possible to add it to if function using "&&"? Thank you.

 public class kick : MonoBehaviour
 {
     private float kickForce = 3;
     public GameObject ball;
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.transform.tag == "player") //could "&&" be added here?
         {
             Vector3 direction = (other.transform.position - transform.position).normalized;
             ball.GetComponent<Rigidbody>().AddForce(-direction * kickForce, ForceMode.Impulse);
         }
     }
 }
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
1
Best Answer

Answer by Hellium · Nov 14, 2019 at 08:51 AM

 public class kick : MonoBehaviour
  {
      private float kickForce = 3;
      public GameObject ball;
      private bool canKick;

      // Call this function in the OnClick event of the UI button
      public void TryKick()
      {
          if (canKick)
          {
              Vector3 direction = (other.transform.position - transform.position).normalized;
              ball.GetComponent<Rigidbody>().AddForce(-direction * kickForce, ForceMode.Impulse);
          }
      }
  
      private void OnTriggerEnter(Collider other)
      {
          if (other.transform.CompareTag("player")
              canKick = true;
      }
  
      private void OnTriggerExit(Collider other)
      {
          if (other.transform.CompareTag("player")
              canKick = false;
      }
  }

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 goutham12 · Nov 14, 2019 at 09:00 AM 0
Share

can you tell me what type of collieder you are using i mean sphere or box

avatar image MrGoldenPorkchop · Nov 15, 2019 at 06:34 PM 0
Share

Thank you so much! I'll try it

avatar image
1

Answer by dacarrera · Nov 14, 2019 at 01:32 AM

I think you have a good start and you're thinking about the problem in a good way. However, for what you want to do, OnTriggerEnter() isn't the best approach. It's functionality is only called when the Collider enters the trigger boundary, not every frame while it's inside. So you'd actually want to check if your player is in range (rather than just entering a trigger once). You have different options and should think about what would be best for your game:

  • There's an OnTriggerStay() docs ref that is called just about every frame that the collider is touching the trigger.

  • You could just check if the player is in range by comparing transform.positions.

It really just depends how you have your scripts setup/organized. If you have an actual "kick" button then you don't need to be always checking for range since you can just check when "kick" is called. If you want your Kick button to become available when the player is in range, that's when OnTriggerEnter() can become an option, since you can make the Kick button visible once the player is in range. Just try to thinking about the step-by-step that happens during your game and it'll help you figure out what is consistent with how your game is being made^^

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 MrGoldenPorkchop · Nov 15, 2019 at 06:33 PM 0
Share

Thank you so much for the answer and for the tips! I really appreciate your help

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

304 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 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 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

Creating dynamically moving buttons/GUI objects. 1 Answer

[SOLVED] GUI Button Rotate animation Unity 5 1 Answer

is it possible to have two on click events in one button 1 Answer

on and off music button in unity 5 0 Answers

How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want. 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