- Home /
Detecting if near the edge of an object ?
Is there a way to detect if Rigidbody is near the edge of an object. For instance a stadium if its near the edge it will try to go to the center.. something like that ?
Answer by Tasarran · Dec 07, 2011 at 07:56 PM
If the arena is circular, you could do it two ways that I can think of right off the bat:
1) Make one big Sphere Collider, and use it in reverse: check to see if you are in it, if you Leave it, trigger. (this might not quite work, but throwing it out there for brainstorming reasons)
2) You could have an object (an empty GameObject will do), and constantly measure the distance to that object. This should work fine, and has the benefit of being able to work at multiple ranges.
1)Wow thats a totally new thing! 2)Where to place that empty gameObject ? If at center then it will calculate the distance of the enemy from center just like radius and i can do something like if distance is greater than 10(or so) then move to the center.. looks like i got it thanks
That's exactly what I had in $$anonymous$$d on #2, I agree it looks like you got it :)
Answer by Kacer · Dec 06, 2011 at 11:08 AM
You can add Colliders to the edge of the arena/stadium, then use a raycast to determine how far you are from the edge.
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
or you could place said colliders on the edge of the arena itself, and then when the object enters it, tell it to go the other way, or to a certain position.
the stadium is circular so how can we add colliders to edge ? how can we map distance between multiple objects and single center ? damn AI thing
if the stadium is circular, just compute the distance from it's center and compare it to the arena's radius...
how to can i measure distance from collider (not transform but collider ) because i created a empty gameObject and added a wheel collider to it and set it up at the edges just like the boundary line now i would like to measure distance from it
Hold Stop.
Wheel colliders is not something used for walls of a circular arena;
http://unity3d.com/support/documentation/Components/class-WheelCollider.html
What you could do is to make a model of the edge of the arena and use a mesh collider on it.
Or you could put up a load of box colliders, but really, by having a circular arena you're making everything much more complicated :P
Your answer
Follow this Question
Related Questions
Collision Enter one of the objects ? 1 Answer
Collision on character controller (fps player) 0 Answers
How to do collision detection? 1 Answer
Collision Detection 2 Answers