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 /
avatar image
0
Question by ChippedChap · Dec 23, 2015 at 10:01 AM · collisionphysicsrigidbodycollidertrigger

How do i get OnTriggerStay2D to work?

Hello! I'm trying to get an object (Lets call this one the 'Planet' object) to gravitate towards another object (Lets call this one the 'Sun' object). The way i did this is to have a trigger collider in the Sun object much larger than it and when the Planet object is inside said trigger collider, a force is applied to the Planet object towards the Sun object. However i'm trying to use OnTriggerStay2D to detect if the Planet object is in the trigger or not, but i cannot for the life of me figure how to get to work. The problem is that the OnTriggerStay2D function is not running at all, i know this because i have a Debug.Log function that displays text when it should be detecting something, but it's not showing up in the console. Any pointers?

I know that other people had problems with OnTriggerStay2D and it's variants, but i've read them but it's still not working. Here is what i tried so far.
1. Both the Sun and Planet objects have Rigidbody2Ds on them, with the Sleeping Mode set to 'Never Sleep'
2. The Sun's rigidbody is Kinematic (I want it to be static for simplicity).
3. Gravity is disabled.
4. Each object is attached to thier own individual empty gameObjects
5. I am using 2D colliders/triggers for both objects.

Here is my code, it is attached to my Sun object.

 using UnityEngine;
 using System.Collections;
 
 public class GravityRuntime : MonoBehaviour {
     Rigidbody2D affected;
     private Vector2 gravityDir;
     public bool debugLines;
 
     void Start () {
         Debug.Log ("Starting");
     }
 
     void OnTriggerStay2D (Collider2D influenced) {
         Debug.Log ("Collider Detected");
         affected = influenced.GetComponent<Rigidbody2D> ();
         Vector2 influencepos = influenced.transform.position;
         Vector2 starpos = gameObject.transform.position;
         gravityDir = new Vector2(influencepos.x - starpos.x, influencepos.y - starpos.y);
         if (debugLines == true) {
             Gizmos.DrawRay(affected.transform.position, gravityDir);
         }
         affected.AddForce (gravityDir * 10); 
     }    
 }
 

(Note: I know gravity falls off by the inverse square law, but the AddForce part is a placeholder.)

So, what do you think? Keep in mind that this is my first question and i probably missed some useful guidelines, if so please feel free to tell me.

Have an excellent day!

Comment
Add comment · Show 1
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 wibble82 · Dec 23, 2015 at 11:51 AM 1
Share

Couple of questions:

  • Your sun's collider should be marked as a trigger, and your planet should not be marked as a trigger. is this the case?

  • Silly question, but are all your objects definitely using 2D physics - i.e. they have 2D rigid bodies, and 2D colliders?

  • Could you try starting your planet outside of the sun's trigger, then moving it inside while the game is running. This could be an issue with it starting inside the sun's trigger - worth checking.

-Chris

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Leuthil · Dec 23, 2015 at 03:22 PM

I believe this is the cause of your issue.

Since your sun will not move (kinematic) and you have a gravity scale of 0, you can remove the Rigidbody from the sun which would make it a static body and then I think everything should work as intended. That is, provided that you are doing everything else correctly as @wibble82 questioned you about.

More information according to a comment in the link above:

In Box2D kinematic bodies only collide with other kinematic bodies, therefore the second static body doesn't create a contact.

When you change the first body to be dynamic (un-check the IsKinematic property), then it's a dynamic body.

  • Kinematic can collide with kinematic.

  • Dynamic can collide with static and dynamic.

  • Static can collide with dynamic.

Apparently Box2D imposes this and not Unity.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

overlapsphere to destroy NPCs on exit 1 Answer

How to get collision point when using onTriggerEnter 5 Answers

Prevent shooting when gun is inside wall 1 Answer

Removing bump when going through two adjacent colliders 2 Answers

Layers are colliding even though they are set to ignore each other 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