I am generating a ‘shield’ along a moved finger touch. Similar to Fruit Slice, but my ‘shield’ persists on the screen until destroyed or another line is drawn.
I am having some issues with adding BoxColliders to this for collisions. Boxes are added, but if you draw diagonally, a huge box is drawn, and things are colliding before reaching the drawn LINE because they are hitting the large box. I have tried EdgeCollider and others but they stop being rendered and added if the shield is a little long.
One option is to do a raycast from first touch point to last to check for collisions, so it would be more exact. I’ve tried and I’m not sure how to do this, any assistance would be great! I just want a fast, responsive line.
Thanks
This is how you create the ray from the start and end points on your line then raycast and find all of the objects hit. Let me know if you’d like me to explain anything in more detail:
Vector3 StartPoint; // I'm assuming you already have this
Vector3 EndPoint; // I'm assuming you already have this
Vector3 VecBetween = EndPoint - StartPoint; // This gets a vector from the start point to the end point
Ray ray = new Ray (StartPoint, EndPoint - StartPoint);
RaycastHit[] RayHits = Physics.RaycastAll (ray, VecBetween.magnitude);
foreach (RaycastHit hit in RayHits)
{
GameObject objectTouchingLine = hit.collider.gameObject;
// do stuff with the object you've found here
}
That looks pretty good. However, the line won’t always be straight. Finger tracing could be angular or curved, so I would need create a raycast from certain points along the touch positions. And I’m not sure how to get points along a line Renderer…
Ahh, well I don’t recommend raycasts for complex lines, as you’ll need several and raycasts are too expensive to be doing too many per frame. You’ll have to get creative, maybe you could make the line a series of short rectangles with box colliders and rotation.
They aren’t mega complex. But usually they are curved or something. I am currently making boxes to match the transform of the line. Problem is a long diagonal line creates a large box that isn’t that tight to the line. Not sure how to go around this in code.