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).
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?
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...
Ok, sounds like you should use OnTriggerExit ins$$anonymous$$d then. I'll post an answer here shortly :)
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);
}
}
}
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));
}
}
}
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...
Your answer
Follow this Question
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