Convert transform.position to Local Space
I need to convert transform.position.y to compare with Camera.main.pixelHeight/2. My code is if (transform.position.y>Camera.main.pixelHeight/2) but of course I cant get the required result because Im trying to compare a World Space Coordinate with a Local Space Coordinate. Any help would be appreciated.
Transform.TransformPoint and Transform.InverseTransformPoint are what you are likely looking for.
Though maybe you are trying to check whether and object is on screen? in which case you can use Camera.WorldToScreenPoint or Camera.WorldToViewportPoint
@Scribe Im just trying to add some enemies, when they enter the screen view from the top they are moving at a slower speed. Then when they cross the half height of the screen(going downwards) they move at a higher speed and leave the screen from below. This was done in a game called Ge$$anonymous$$i for iOS and Android.
If you want the position they are on screen you should use the second functions I mentioned:
Vector3 screenPos = Camera.main.WorldToViewportPoint(yourEnemy.transform.position);
if(screenPos.y > 0.5){
//do something
}else{
//do something else
}
Answer by Landern · Sep 01, 2015 at 12:41 PM
Transform.InverseTransformPoint, the example should be enough to get you going from the script API.
@Landern Does not work. Whats wrong :(
int screenHeight = Camera.main.pixelHeight;
Vector3 targetPos = new Vector3(transform.position.x,-10.0f,0f);
Vector3 convertWorldToLocalPos = transform.InverseTransformPoint(transform.position.x, transform.position.y, transform.position.z);
if (convertWorldToLocalPos.y>screenHeight/2) {
float move = slowSpeed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPos, slowSpeed*Time.deltaTime);
} else if (convertWorldToLocalPos.y<screenHeight/2) {
float move = fastSpeed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPos, fastSpeed*Time.deltaTime);
}
Use the cameras transform, check out the API. Also you're not doing anything interesting to the transforms in the 3 parameters override, so use the one that only takes the full Vector3 as below. Also base it off the Camera's transform.
Yours:
Vector3 convertWorldToLocalPos = transform.InverseTransformPoint(transform.position.x, transform.position.y, transform.position.z);
$$anonymous$$ine, may work?:
Vector3 convertWorldToLocalPos = Camera.main.transform.InverseTransformPoint(transform.position);
@Landern for some reason it doesn't work. Im not quite sure why but WorldToViewportPoint as mentioned by Scribe works perfectly. Im sure Im making a mistake somewhere with InverseTransformPoint but anyways thanks for help :)
Your answer
Follow this Question
Related Questions
How to attach an object to another object so that moving one moves the other 0 Answers
Get Random transform point inside collider 0 Answers
make a gameobject move to another gameobject that was selected by mouse click? 1 Answer
Trying to transform my character's local position on a GetKey. 0 Answers