- Home /
TransformPoint returns wrong points?
I have some code where I take a global position, convert that with InverseTransformPoint to local. I then split that variable up into two variables, one subtracts from it and the other adds to it (the same value is subtracted/added). When this stage is done the difference between the two, locally, are (0.2,0.2,0.0). However, after converting it back to global the difference between the two are (-4.7,-0.8,-0.8).
I'm aware it's probably just my math or thinking that is off, but I thought I'd ask nonetheless. Does it return wrong points? Or is there some weird voodoo ritual I gotta do first?
Here's the code I use for converting and tweaking (info is a RaycastHit, radius is a Vector3):
Vector3 localUpperRightCorner = (transform.InverseTransformPoint(info.point)+radius);
Vector3 localLowerLeftCorner = (transform.InverseTransformPoint(info.point)-radius);
Vector3 upperRightCorner = transform.TransformPoint(localUpperRightCorner);
Vector3 lowerLeftCorner = transform.TransformPoint(localLowerLeftCorner);
localUpperRightCorner - localLowerLeftCorner = (0.2,0.2,0.0)
upperRightCorner - lowerLeftCorner = (-4.7,-0.8,-0.8)
X&Y on the latter is supposed to equal the previous one's.
One of my comments earlier:
Yes, I use a different scale. What I need done is to just plot out a small distance from one corner of the object to the other. The distance totaled to (0.2,0.2,0). This distance was plotted out on the surface of another object (thus the raycasthit). then I took those coordinates and made them into global, and they don't align themselves to what they should've. Which is the issue I'm facing.
Answer by robertbu · May 25, 2013 at 06:35 AM
I assume that radius is a Vector3. I put the following code in Update() and played with the position and rotation of the game object. The magnitude between the local and global coordinates was always the same. But if I change the Scale of the game object, the values were no longer the same, which makes sense. I'm guessing that the game object you have the script above attached to has a scale other than (1,1,1).
if (Input.GetKeyDown (KeyCode.A)) {
Vector3 localUpperRightCorner = (transform.InverseTransformPoint(transform.position)+radius);
Vector3 localLowerLeftCorner = (transform.InverseTransformPoint(transform.position)-radius);
Debug.Log ("Local Distance = " + (localUpperRightCorner - localLowerLeftCorner).magnitude);
Vector3 upperRightCorner = transform.TransformPoint(localUpperRightCorner);
Vector3 lowerLeftCorner = transform.TransformPoint(localLowerLeftCorner);
Debug.Log ("world Distance = " + (upperRightCorner - lowerLeftCorner).magnitude);
}
Yes, I use a different scale. What I need done is to just plot out a small distance from one corner of the object to the other. The distance totaled to (0.2,0.2,0). This distance was plotted out on the surface of another object (thus the raycasthit). then I took those coordinates and made them into global, and they don't align themselves to what they should've. Which is the issue I'm facing.
You are going about this the long way. First, '`transform.InverseTransformPoint(transform.position)`' will always equal (0,0,0). Second the corners you are calculating for a cube will always be (0.5,0.5,0.5) and (-0.5, -0.5, -0.5) regardless of the scale of the cube. So you can do:
Vector3 corner = new Vector3(.5f, .5f, .5f);
Vector3 upperRightCorner = transform.TransformPoint(corner);
Vector3 lowerLeftCorner = transform.TransformPoint(-corner);
Debug.Log ("LL: "+lowerLeftCorner+" UR: "+upperRightCorner);
Debug.Log ("Distance: "+(upperRightCorner - lowerLeftCorner).magnitude);
But for your original code to find the corners, your radius Vector3 would have to equal transform.localScale / 2.0 and be in global coordinates, so you don't have to do the TransfromPoint()
at all. You can do:
upperRightCorner = transform.position + radius;
lowerLeftCorner = transform.position - radius;
Debug.Log ("LL: "+lowerLeftCorner+" UR: "+upperRightCorner);
Debug.Log ("Distance: "+(upperRightCorner - lowerLeftCorner).magnitude);
I do do radius = localscale/2. And I haven't gotten the local coordinates of the object itself. The object is trying to get the local coordinates of a raycasthit (and it does). That way I can use local coordinates±radius to get where the corners of a square would be along the 1st object (the one that got hit by the raycast) itself, ins$$anonymous$$d of calculating the 1st object's rotation to get where the corners would be located.