- Home /
Get gameobject limits
Im using a raycast to detect if a gameobject is between the target, what I want now is to get the distance between the point of the object hit and the left and right points where the object ends. I attach an image for a better understanding here. Thanks for the help and tell me if you can't understand the question (I dont know exactly how to explain it but I can attach a more precise screenshot).
Are you referring to the bounding box or the mesh and left and right points where the object ends?
Do you know the size of the 'wall' with a localScale of (1,1,1). Is the 'wall' scaled? Are the arrows in your drawing always across a particular local axis (i.e. the 'x' axis of the object)?
I tried using the bounding box but when I rotated the wall across the 'y' axis I didn't get the results I was expecting from getting the max and $$anonymous$$ bounds.
The arrows depend on the rotation of the object (I guess they can be on the x or z axis depending on its rotation).
I forgot to say that what I want to get is the point at which the collider attached ends (like drawing a line perpendicular to a raycast recieved to the center of the collider until you reach the end of the box), thanks!
Answer by robertbu · May 09, 2014 at 08:37 PM
You can use local coordinates and mesh.bounds to find these edges. Here is an example script. Note it uses the alignment between the normal and the forward to detect whether you clicked on a back/front side or a left/right side. This will not work for an arbitrary shaped object...just cubes.
To test, start a new scene. And and resize a cube. Add the script to an empty game object. Click on the cube. It positions a couple of marker spheres on the edges and outputs in a Debug.Log(), the distance to the two edges:
#pragma strict
private var marker1 : Transform;
private var marker2 : Transform;
private var localXExtent : float;
private var localYExtent : float;
function Start() {
marker1 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
marker2 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
marker1.localScale = new Vector3(0.15, 0.15, 0.15);
marker2.localScale = new Vector3(0.15, 0.15, 0.15);
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
var target = hit.transform;
var extent : float;
var mf = target.GetComponent(MeshFilter);
var localHit = target.InverseTransformPoint(hit.point);
if (mf != null) {
if (Mathf.Abs(Vector3.Dot(hit.normal, target.forward)) > 0.9) {
extent = mf.mesh.bounds.extents.x;
marker1.position = target.TransformPoint(Vector3(-extent, localHit.y, localHit.z));
marker2.position = target.TransformPoint(Vector3(extent, localHit.y, localHit.z));
}
else {
extent = mf.mesh.bounds.extents.z;
marker1.position = target.TransformPoint(Vector3(localHit.x, localHit.y, -extent));
marker2.position = target.TransformPoint(Vector3(localHit.x, localHit.y, extent));
}
Debug.Log("Distance 1: "+Vector3.Distance(marker1.position, hit.point));
Debug.Log("Distance 2: "+Vector3.Distance(marker2.position, hit.point));
}
}
}
}
Works almost perfectly!, I found some issues with the script when the hit point was on edges, but thank you a lot!
Your answer
Follow this Question
Related Questions
Plane geometry 0 Answers
Direction of a moving object 1 Answer
Get mapping postion of 1 point on a plane defined by 3 points 1 Answer
Tranposing two 3D coordinates onto a 2D plane? 1 Answer
How to find acute and obtuse angle between 2 points 1 Answer