Finding Boundaries of a Cover Object
I am working on a stealth game. The cover objects are giving me issues though. I am using lots of empty GameObjects for detecting the cover object and defining their bounds. This should be a simple issue that could be resolved during level creation but in the future I'd like to incorporate a way for players to make levels as well, so GameObjects labeled with a tag "Cover" are simply attached to prefabs that I'm using to create the levels.
Given this picture, where the blue is the player, the red are cover points, and the black is wall, I need a way to constrict the player to the boundaries of the wall using the cover points. I am completely at a loss though. I will say that I had originally used colliders on the walls but it was slow and didn't work with the level creation system.
My first thought was to check the rotation of the closest cover point, then find the closest two points that are facing the same direction that are at the end of an object. Two problems arose with that idea: I don't know how to find the end points, and there are times when the closest end points would be the wrong end points (as in the example below).
My most recent thought is to check the rotation of the prefab, then check for objects with tag "Cover", then take objects on that list that have the same rotation and either x or z position (depending on rotation) as the prefab. From there I'd find the farthest to the left and right of the point and save that. Then I could just use those saved coordinates any time that point is used for cover. This idea seems to have potential but I don't know how I'd stop at the end of the wall rather than just going to the farthest possible point.
If anyone has any ideas on how I can accomplish this, I would very much appreciate it, because I've been stuck on this problem for over a week and have no ideas on how to fix it.
Thanks!
Answer by Rangr · Nov 27, 2015 at 07:31 AM
If anyone cares to know, I figured it out. Here is the code I used:
List<GameObject> lefts = new List<GameObject>();
List<GameObject> rights = new List<GameObject>();
GameObject[] obj = GameObject.FindGameObjectsWithTag("Cover");
foreach(GameObject go in obj){
if(go.transform.position != transform.position && (Mathf.Round(go.transform.eulerAngles.y) == Mathf.Round(transform.eulerAngles.y) && Mathf.Round(transform.eulerAngles.y) == 0f))
{
if(go.transform.position.x == transform.position.x)
{
if(go.transform.position.z < transform.position.z)
{
if(FindClosest().transform.position != transform.position)
{
lefts.Add(go);
}
//at the end, find the closest from the list of ends, that is coverpoints 1, and second closest which is leftJumpPoint
}
else if(go.transform.position.z > transform.position.z)
{
if(FindClosest().transform.position != transform.position)
{
rights.Add(go);
}
}
}
}
else if(go.transform != transform && (Mathf.Round(go.transform.eulerAngles.y) == Mathf.Round(transform.eulerAngles.y) && Mathf.Round(transform.eulerAngles.y) == 180f))
{
if(go.transform.position.x == transform.position.x)
{
if(go.transform.position.z > transform.position.z)
{
if(FindClosest().transform.position != transform.position)
{
lefts.Add(go);
}
//at the end, find the closest from the list of ends, that is coverpoints 1, and second closest which is leftJumpPoint
}
else if(go.transform.position.z < transform.position.z)
{
if(FindClosest().transform.position != transform.position)
{
rights.Add(go);
}
}
}
}
}
plus an if for 90 degrees and 270 degrees.