hi, i was wondering if there is a way to detect the distance between an object i am looking at and the other object behind it with a raycast? for example i whant to look a a crate and when i press lets say “E” it check if the object is let say 5 unit or closer to the crate and if it is, it then do someting and all that using raycast. you must keep in mind that they will be more then one crate.
You can easily do it
- Use Physics.Raycast (other.position, Vector3.back, hit).
- Check if hit.gameObject ot transform is the desired one or not. Ex: if (hit.gameObject.tag == “target”)
- get the distance from hit.distance property
- Thats it