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 mrsev · Oct 26, 2016 at 01:47 PM · collisionrigidbodyaddforce

Apply Force when a Rigidbody ExitCollider

Hi there !

It might sound extremely simple, but I searched for LOTS of forums and tutorials and can't get it right.

I just want my little player there get a big gravity like force on its' Rigidbody when he leaves the "secretion" (the lake on the picture).

I have absolutely zero response from the code on ExitCollider. (Tried various other codes than this one).

alt text

alt text

unity.jpg (310.3 kB)
code.jpg (218.4 kB)
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 conman79 · Oct 26, 2016 at 08:44 PM 0
Share

The first problem is Line 11 is missing the brackets after GetComponent(). It should read

 GetComponent<Rigidbody>().AddForce(Vector3.up * -10, Force$$anonymous$$ode.Acceleration);

But you are going to run into other problems because you have your OnCollisionExit set up wrongly too.

But before I tell you how to fix that I need to know whether you should be using OnCollisionExit or OnTriggerExit.

Can you explain how your "secretion" gameobject is set up? Is the collider set as a trigger and the player can move through, or should the player "bounce" off the bottom?

avatar image mrsev conman79 · Oct 26, 2016 at 09:16 PM 0
Share

Thank you much for answering.

It is simply a textured volume, defining the playable area - in fact if the player reaches the summit of it and leaves the volume, I want a vertical force pushing him back down into the volume. Just like on the surface of a lake.

The bottom is a distinct terrain static mesh.

Now it's a cube but it'll be a polygon mesh.

I tried with setting it as Trigger and not trigger.

Should I use collision box or rigidbody as kinematic? Tried both... But it's surely my C# that's messed up.

Also I don't know if I have to assign the script to the secretion or to the characters...

avatar image conman79 mrsev · Oct 26, 2016 at 10:36 PM 0
Share

Ok, sounds like you should use OnTriggerExit ins$$anonymous$$d then. I'll post an answer here shortly :)

2 Replies

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

Answer by conman79 · Oct 26, 2016 at 11:07 PM

Based on the new info you gave, using OnCollisionExit is the wrong way to go about what you are doing here. OnCollisionExit should be used when two non-trigger colliders exit a collision (eg. the moment they bounce off each other).

Even if OnCollisionExit was the right choice, you would be using it wrongly in your code. On line 8, the "Collider secretion" bit doesn't check if secretion is the collider. OnCollisionExit passes on information about the collision and you should use it with a Collision class like this "OnCollisionExit(Collision myCollision)".

OnTriggerExit uses the Collider class eg. "OnTriggerExit(Collider other)", where "other" will simply tell you the collider of the other gameObject.

So let's make this all work! :P

You'll need to set the collider of "secretion" as "Is Trigger". This will work whether you're using a boxcollider like you are now or a mesh collider like you plan in the future.

 using UnityEngine;
 using System.Collections;
 
 public class GravitySecretion : MonoBehaviour 
 {
     public Collider secretion; // Set the secretion collider in inspector
 
     void OnTriggerExit(Collider other) //When this gameobject's collider exits another (trigger) collider, "other" is the other collider
     {
         if (other == secretion) //if the other collider is the same one as "secretion"
         {
             GetComponent<Rigidbody>().AddForce(Vector3.up * -10, ForceMode.Acceleration); 
         }
     }
 }
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 mrsev · Oct 27, 2016 at 01:45 PM 1
Share

Hey ! Thank you for your awesome answer, got it work right assigning it to the hero.

Tweaked it a little to apply a constant force if you fall from a cliff. Works too.

Only thing is that visually it's not looking like floating, but will surely do if I play a little with forces.

 using UnityEngine;
 using System.Collections;
 
 public class GravitySecretion : $$anonymous$$onoBehaviour
 {
     public Collider secretion; // Set the secretion collider in inspector
     private bool outside = false;
 
     void OnTriggerExit(Collider other) //When this gameobject's collider exits another (trigger) collider, "other" is the other collider
     {
         if (other == secretion) //if the other collider is the same one as "secretion"
         {
             GetComponent<Rigidbody>().AddForce(new Vector3(0, -13000, 0));
             outside = true;
         }
     }
     void OnTriggerEnter(Collider other)
     {
         if (other == secretion)
         {
             outside = false;
         }
     }
     void FixedUpdate()
     {
         if (outside == true)
         {
             GetComponent<Rigidbody>().AddForce(new Vector3(0, -5000, 0));
         }
     }
 }
avatar image
0

Answer by mrsev · Oct 30, 2016 at 05:25 PM

Hi ! I made the mesh I want to replace the cube with, for the secretion zone.

It gets tricky because it has more than 256 faces or what and it can't be a collider because of that...

So I'm asking myself should I find another way than OnTriggerExit that doesn't require a collider?

Or is there a way to go around that faces limitation? ... That sucks, I wonder how they do in pro games for like complex rivers or what... Maybe I should just get down the faces count...

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

105 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

Related Questions

How to check fo rigidbody collision? 0 Answers

Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers

Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer

rigidbody.Addforce stacking? 2 Answers

Stop a rigidbody on collide when dragging it with a mouse / with DontGoThroughThings 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