Identifying objects in a stack
Hi all - newbie to Unity/Coding and just trying to get my head around a few things.
I'm creating an AR experience, in which the user interacts with a stack of objects, similar to a Jenga tower.
The type of interaction is dependant on what object is focused on with the camera, and also what side of said object.
I figured to start out with a raycast from the screen centre to hit one of the bricks in the Jenga-like tower:
public float findingBrickDist = .6f;
public float maxRayDistance = 30.0f;
public LayerMask collisionLayerMask;
void Start () {
int layerIndex = LayerMask.NameToLayer ("ARGameObject");
collisionLayerMask = 1 << layerIndex;
}
void Update ()
{
//use center of screen for focusing
Vector3 center = new Vector3 (Screen.width / 2, Screen.height / 2, findingBrickDist);
Ray ray = Camera.main.ScreenPointToRay (center);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, maxRayDistance,collisionLayerMask) && hit.transform.tag == "brick") {
Debug.Log ("Hit a Brick!");
}
The next part is a little difficult for me as I'm trying to decipher what side of the brick is being hit (ie. if it's the short side, that will trigger one interaction. If it's the long side, that will trigger another interaction).
Absolutely stumped on where to go after this. If anyone can point me in the right direction I would be greatly appreciative!
Thanks and hope you're doing well :)
Answer by vinilly · Apr 25, 2018 at 07:50 AM
Can you create an empty game object and assign colliders to the long side, and ends?
...and child that to the game objects. Great tip and yeah that works, however that can be a tedious process when objects of varying shapes are in the works...
I'm just wondering if there's another way around this in code...if not, I'll be on the collider train for sure!
Your answer
Follow this Question
Related Questions
Need help with Ray casting syntax. 1 Answer
Issue with Raycast hit.distance Variables Reseting Itself 0 Answers
Problem dealing with RaycastHit.normal on collider edges 0 Answers
Raycast problem, likely a Unity bug? 0 Answers
I need to know how to spawn a prefab and then have it propelled forward as if thrown 0 Answers