Hi, I have a problem I really cannot figure for a few weeks.
I’m working in 2D.
I am storing a world space position in a Vector2.
I am then creating a new object by instantiating a prefab.
I need the world space position of the previously mentioned Vector2 translated to the local space of the newly created object.
I tried InverseTransformPoint, InverseTransformDirection. I am getting very weird results.
At this point, I would appreciate any advice.
private Vector2 positionOfPlayerAfterPreviousCollide;
...
positionOfPlayerAfterPreviousCollide = myCollisionGameObject.transform.position;
...
impactMarker=Instantiate (prefab, someTouchPoint, Quaternion.Euler (0f, 0f, 90+myAngleDeg));
private Vector2 positionOfPlayerAfterPreviousCollideInLocalSpace; //the positionOfPlayerAfterPreviousCollide translated into the local space of impactMarker
positionOfPlayerAfterPreviousCollideInLocalSpace=???
1 Like
This transforms position from world space to local space and this transforms position from local space to world space.
1 Like
Thank you, Rotary-Heart. The thing is - I tried using Transform.InverseTransformPoint, but it’s giving me ridiculous local coordinates(obviously wrong), I assume because they are affected by rotation and scale.
I need to convert a simple (x,y) point from world to local space. I don’t even know at this point if my approach is correct, that is - is it possible to do what I want(convert point from global to local), or I have to think about the bigger picture and convert a whole TRANSFORM(including position, rotation and scale) from world to local? I tried applying some solutions that I found online for stripping the rotation and scale parts away from Transform.InverseTransformPoint, didn’t really work.
I guess it’s quite obvious by now, but just in case it’s worth mentioning… I’m working with Unity for less than six months, so… sorry for the beginner questions.
In that case all you need to do is subtract the positions.
Vector3 localPos = localObj.transform.position - objToCheck.transform.position;
This will return the position of objToCheck relative to the localObj.
Ok, so I’ve got my object(let’s call it impactMarker) positioned at (-2.1,1.5,0). I tried to convert the position of the origin (0,0,0) to the local space of this object.
impactMarker.transform.InverseTransformPoint (0,0,0) returns (-1540.552,-2165.297,0). I’m definitely missing something.
Are you sure that your object is at that global position? Don’t you have it parented to something that it’s not at 0, 0, 0?
Yes, I’m sure. The impactMarker object has neither parents nor children.
Yeah, you must be doing something wrong, here’s a quick pic showing a test I made
Then here’s the NewBehaviourScript code
public class NewBehaviourScript : MonoBehaviour
{
public Vector3 position;
void Start()
{
position = transform.InverseTransformPoint(0, 0, 0);
}
}
As you can see it doesn’t give me the numbers that you are getting.
Thank you, Rotary-Heart. At least now I have a confirmation of what InverseTransformPoint is supposed to do.
I’ll try to figure out what’s causing my weird returned numbers.