raycast or collision?
hi dudes!
So I'm new to raycasts so forgive me if I'm barking up the wrong tree here.
My idea is that i want the player NOT to move outside of circular boundary, would i be right in saying that a ray cast could work for this kind of thing? If so would i have the raycast attached to the ground and always looking at my player? A raycast is like a laser beam right? Unless there is some sort of circular version?
My other idea was to have a cylindrical collider that the player cant move out of but I'm thinking a raycast is better?
Any advice would be great :)
You may be looking for the wrong feature to solve your problem. If it is a circular boundary the max distance is the same on all directions, you should check if the player is in between the center of the circular boundary + the maximum distance from the center.
Answer by rageingnonsense · Oct 21, 2015 at 11:13 PM
Let's assume your circular boundary's origin is defined by a Vector3 called "origin", and the radius of the boundary is 100 units. You can just do the following test each frame:
if(Vector3.Distance(player.transform.position, origin) > 100) {
//out of bounds
}
You COULD use a ray for this, or colliders; but this will be faster honestly. In fact, you can make it even FASTER if you the circular boundary's origin happens to be the world origin (0,0,0). In that case, it is simply this:
if(player.transform.position.magnitude > 100) {
// out of bounds
}
This is because magnitude is nothing more than the distance from the world origin.
thank you so much! I'm sorry its taken so long to get back to you, had a lot on and my internet cable was literally dragged down the street by a truck Lol!
Your above suggestions worked perfectly! Thanks again!! ;)