- Home /
Check if GameObject is within the volume of a cone.
Currently, I'm making an adventure game similar to Myst. The only issue I'm having is the raycast used to tell if the player is looking at a switch or button is too precise to feel good without a reticle. To solve this I'm moving towards testing if a point on an object is between 4 points on a cone.
I've got the 4 points moving about the enviroment based on the direction the player is facing, and the only thing I'm not that sure on is the math for this test. Should I take the cross product between 2 points and if those 2 satisfy the condition check the next two? Maybe a pyramid is a better shape to use.
Answer by meat5000 · Jun 22, 2014 at 04:44 PM
Oh wow, that would be even easier. I don't need all of my currently active buttons to register with the script responsible for notifying them on an intersection. Thanks.
Answer by Tortuap · Jan 15, 2021 at 10:09 AM
For anyone looking for a is point inside cone test routine :
public static bool IsPointInsideCone ( Vector3 point, Vector3 coneOrigin, Vector3 coneDirection, int maxAngle, int maxDistance )
{
var distanceToConeOrigin = ( point - coneOrigin ).magnitude;
if ( distanceToConeOrigin < maxDistance )
{
var pointDirection = point - coneOrigin;
var angle = Vector3.Angle ( coneDirection, pointDirection );
if ( angle < maxAngle )
return true;
}
return false;
}
Answer by robertbu · Jun 22, 2014 at 04:49 PM
The typical solution to the problem is to put a collider on object like switches and raycast against colliders on these objects. When the player's raycast hits a collider of a particular type, the code tells the object, and the object does whatever it is programmed for. If you want to detect in a cone fashion as you've asked:
'player' is the transform of the player
'object' is the transform of the object being tested
'r' is the radius of the code
'look 'is a direction vector of the center of the reticle. If you are constructing a ray from the mouse position, it will be the Ray.direction.
var v = object.positon - player.position; var maxAngle = Mathf.ATan(r / v.magnitude) * Mathf.Rad2Deg;
if (Vector3.Angle(look, v)
Your answer
Follow this Question
Related Questions
How to scale/calculate GameObject Z distance to the real physical world... 0 Answers
Movement with playmaker 0 Answers
Error downloading Unity 2 Answers
School Assessment 2 Answers
Volume detection. 0 Answers