Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ekta-Mehta-D · Jul 29, 2015 at 10:07 AM · unity 5collisioncollider

Check Object Is Not Colliding

Hii..

I have one gameObject with jump animation.

I want to check while end of jump animation whether gameObject is colliding or not. If gameObject collides with collider then object will move forward otherwise with gravity force , it will fall down.

But i stuck at one point , How do i find object is not colliding with collider?

Code :

 void Update () {
 
         if (moveForward) {
             transform.Translate (Vector3.right * Time.deltaTime * speed, Space.World);
         } else {
             transform.Translate (Vector3.right * Time.deltaTime * speed, Space.World);
             this.GetComponent <Rigidbody>().useGravity = true;    
         }
             
         if(this.transform.position.y < -5.00f){
             Destroy(this.gameObject);
         }
 
     }
 
     void OnCollisionEnter(Collision collision) {
 
             if (collision.gameObject.tag.Equals ("platform")) {
                 
                 moveForward = true;
             } 
         }

Here . OnCollisionEnter will call when object collides with platform but what if object does not collide with any of the object. At that time OnCollisionEnter will not be called so that i can set moveForward = false.

Even i have set event at end of the jump animation. Now i can get the point when player will come down while jumping.

Still i am stuck at this point. anyone please help me out. I need ur big help.

Thanks.

Comment
Add comment · Show 3
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 Positive7 · Jul 29, 2015 at 10:14 AM 0
Share

I'm not sure if I understand it right so I'll put this as a comment But shouldn't this solve it? void OnCollisionEnter(Collision collision) {

              if (collision.gameObject.tag.Equals ("platform")) {
                  moveForward = true;
              } else{
                  moveForward = false;
              }
          }

avatar image Eno-Khaon · Jul 29, 2015 at 10:23 AM 0
Share

This would only trigger upon first contact with a collider. Unfortunately, this doesn't help when the goal is to deter$$anonymous$$e when there are no active collisions with any objects during any given frame.

avatar image Ekta-Mehta-D · Jul 29, 2015 at 10:25 AM 0
Share

But if collider is not available at that place then OnCollisionEnter will not be called. and at that time i want to give gravity to object.

2 Replies

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

Answer by Eno-Khaon · Jul 29, 2015 at 10:20 AM

A quick and easy solution (possibly needing some fine-tuning, depending on how it's put together) would be to use a variable to determine whether you're presumably on the ground or not.

 bool isGrounded = true;
 
 // ...
 
 void OnCollisionStay(Collision other)
 {
     if(other.CompareTag("platform"))
     {
         isGrounded = true;
     }
 }
 
 void OnCollisionExit(Collision other)
 {
     if(other.CompareTag("platform"))
     {
         isGrounded = false;
     }
 }

Then, your accommodations for moveForward can be based on the "isGrounded" variable. This means you'll need to ensure that "isGrounded" is always accurate, but if you're looking to test for an event based on whether the event is occurring or not, this is a way to handle it.

As a side note, I used "CompareTag" in the if statements to improve performance.

Comment
Add comment · Show 6 · 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 Ekta-Mehta-D · Jul 29, 2015 at 10:38 AM 0
Share

If collider is not available when object is grounded then this OnCollisionStay or OnCollisionExit will be called ?

avatar image shriya · Jul 29, 2015 at 10:46 AM 0
Share

No , collision methods wont be called but boolean will maintain the states

avatar image Ekta-Mehta-D · Jul 29, 2015 at 10:48 AM 0
Share

This solution will not work . $$anonymous$$y object will be in air while jumping and at that time isGrounded will be false. and it will fall down.

avatar image Ekta-Mehta-D · Jul 29, 2015 at 10:51 AM 0
Share

I want to check at animation end object is colliding to the ground or not ?if not then how can i detect that ?

avatar image shriya · Jul 29, 2015 at 10:55 AM 0
Share

yes it would be false, but if it collides then it will start moving forward else it would keep falling down. Thats what needed i guess.

$$anonymous$$aybe you need to specify more to make others understand what exactly is needed .

Show more comments
avatar image
0

Answer by mightyBearArthur · Sep 27, 2016 at 06:15 PM

If you want to fire event exactly when no collisions happend you need to create a monobehavior class that will keep a bool property (for instance "CollissionDetected") whether any collision on an game object happend or not and set it to "false" by default. Then create another monobehavior class and attach it to the game object which you whant to check collisions (or no collision) for. In that class use OnCollisionX or OnTriggerX events. Each event has to change you bool property according to it's goal (for instance OnCollisionEnter, OnCollisionStay or OnTriggerEnter, OnTriggerStay should set bool property to "true" and in OnCollisionExit or OnTriggerExit events set it to "false"). Then in the first class with "CollissionDetected" property in LateUpdate() monobehavior event your bool property will tell you whether any collisions happend to your game object or not. To make it more clear see unity Execcution order diagram - https://docs.unity3d.com/Manual/ExecutionOrder.html (Scroll down to diagram). As you can see all physics that include Collisions and Triggers events are processed before LateUpdate is called. if you are interested why not Update() ... So one of the reason is that Update() you can use for you game logic. And the seccond i call it Magic or maybe i still didn't understand all of the things :D For me that works perfectly!

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

24 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

Related Questions

Disabling and Enabling a collision 1 Answer

How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers

Can you help me about collision please ? 3 Answers

Detecting collision of two game objects? 0 Answers

Box and circle Collider doesn't work 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