- Home /
Getting the radius of a square
Hi, i am trying to get the radius of a square so that an object can be placed a certain distance away from the square. I have this but it does not work:
Vector3 differenceFromCenter = hit.point - hit.transform.position;
Vector3 SecondPoint = hit.transform.position + (differenceFromCenter.normalized * (differenceFromCenter.magnitude+distance));
where hit is a RaycastHit which hits the square and distance is added distance away from the square. How can this be done?
Answer by TeoL · Jul 29, 2014 at 12:24 AM
Could you do something like this?
Vector3 locationToPlaceObject;
Vector3 distance = new Vector3(10,0,0);
Vector3 objectEdge = hit.transform.position - hit.transform.bounds.extents;
locationToPlaceObject = objectEdge - distance;
Would that would on the if the ray hit the bottom or top of the cube?
Answer by VesuvianPrime · Jul 29, 2014 at 12:05 AM
How are you calculating distance at the moment?
There are a number of different ways you can get the dimensions of your hit object, varying in accuracy:
1) Assuming the object is a perfect Cube you can take the lossyScale/localScale of the transform as its world dimensions (e.g scale 10x10x10 would be a cube of side 10).
2) Since you're using rays already, we can take the collider of the target GameObject and look at the bounding box for dimensions.
3) You could even get the MeshFilter from the target GameObject and get the bounds from the Mesh
The Distance is a fixed value, for example: 1. I forgot to mention that in the question. What i am trying to achieve is to position an object a distance away from the cube. If the distance is 1, it would be 1 distance from the cube, not the position of the gameobject. There is an image of what i am trying to explain. The red line shows the points in which an object could be created. If a ray hits the cube at "X" from the right then the object would be positioned at "Y". hope this explains it better!
Your answer
Follow this Question
Related Questions
magnitude normalize help 1 Answer
Jumping problem to do with /= magnitude changing the vector3 direction 1 Answer
Problems with diagonal movement. 1 Answer
getting the radius of a capsule collider 1 Answer
radiusX and radiusY ? 1 Answer