x


Check if position is inside a collider

Hey all,

This is for a teleport feature.

I am about to write a function that returns if a position is inside of a collider.That way I can negate the teleport, so the player doesn't go inside of objects.

Does unity have some magic way of doing this? I have an idea of how I can do it, but I wanted to ask here first in case there was an built in way / simple way one of you guys knew of.

Thanks

more ▼

asked Jan 02 '11 at 09:46 PM

s4vi0r gravatar image

s4vi0r
298 3 3 13

What do you mean by negate the teleport?

Jan 02 '11 at 09:54 PM Socapex

On collision, make the player's transform (Position) where you want it to be... If you want a random position, you can make it randomly select it from a couple of positions (Use empty gameobjects perhaps to get it, then you just move the player to that spot...).

Jan 02 '11 at 09:59 PM Justin Warner
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Guys I found a good simple way of doing this. Thanks for the responses. Here is a hunk of code

I used a sweeptest on my rigidbody, I used distance and direction of my teleport location and used it as the ray to send out. I tested the what my RaycastHit got assigned to like this.

if(hitToTest.collider.bounds.Contains(telePosition))
{
   print("point is inside collider");
}

Check out unity SweepTest() and Collider.bounds for specific syntax and what not

more ▼

answered Jan 02 '11 at 10:41 PM

s4vi0r gravatar image

s4vi0r
298 3 3 13

Hey, great to report your solution, but you should really mark your question as answered (using your answer). Cheers

Jan 02 '11 at 10:56 PM Socapex

I tried already. Says I have to wait 47 hours for some reason )=

Jan 02 '11 at 11:18 PM s4vi0r

Oh yes thats true :) Srry

Jan 03 '11 at 12:02 AM Socapex

I believe `bounds` is always just a cube, so it might not be exactly what it seems (the precise collider mesh boundary). Try this instead: http://answers.unity3d.com/questions/163864/test-if-point-is-in-collider.html

Nov 27 '12 at 05:42 PM Cawas
(comments are locked)
10|3000 characters needed characters left

You can use triggers to check if objects are colliding. In the collider inspector of your object, turn on 'Is Trigger'. Then you can create a script on your object that sends out values using the OnTriggerEnter(){}, OnTriggerStay(){} and OnTriggerExit(){} functions.

Edit: You could make your teleport OnTriggerEnter(){} function modify the location of the character easily. You can access your players location from that function and change it.

Make sure one of the colliding objects is a rigidBody, since it wont trigger with an other static collider. Checl the Collision action matrix here for more information on triggers and what will trigger them.

Also, the 3d platform tutorial covers teleports IRC. Their solution might be more complete than mine, can't remember how they did it though.

more ▼

answered Jan 02 '11 at 10:00 PM

Socapex gravatar image

Socapex
57 5 7 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1697
x1529
x1361
x885
x193

asked: Jan 02 '11 at 09:46 PM

Seen: 7998 times

Last Updated: Nov 27 '12 at 05:57 PM