- Home /
Code to detect side of an object a point is on is offset
I am using a raycast to determine when a player has clicked on an object, so that they can connect two objects together. While I can just let them try to line up the objects, I think it would be better if they were able to "snap" the objects into a correct alignment.
Therefore, I have used the following code to do this: public Vector3 GetSide(Vector3 CheckPoint) { Vector3 Localized = transform.InverseTransformPoint(CheckPoint); Vector3 Direction = new Vector3();
float XDifference = Mathf.Abs (Localized.x);
float YDifference = Mathf.Abs (Localized.y);
float ZDifference = Mathf.Abs (Localized.z);
if ((XDifference > YDifference) && (XDifference > ZDifference))
{
if (Localized.x < 0)
Direction = -transform.right;
else if (Localized.x > 0)
Direction = transform.right;
}
else if ((YDifference > XDifference) && (YDifference > ZDifference))
{
if (Localized.y < 0)
Direction = -transform.up;
else if (Localized.y > 0)
Direction = transform.up;
}
else
{
if (Localized.z < 0)
Direction = -transform.forward;
else if (Localized.z > 0)
Direction = transform.forward;
}
return transform.position + Direction * (transform.localScale.x + 0.01f) / 2;
}
It mostly works, however instead of giving an exact point, it is slightly offset, so the attached part is not centered accurately. Any ideas as to why this is?
Thanks!
Your answer
Follow this Question
Related Questions
Offset of texture with Raycast 1 Answer
How to get an offset of a RaycastHit? 0 Answers
raycast object instance offset? 1 Answer
Raycast refuses to cast down 1 Answer
Detemine wich side of a cube was hit 1 Answer