- Home /
Object position
How can I determinate is the object on the left half of the screen or on the right side. What is procedure for doing it.
Answer by Wolfdog · Jun 27, 2015 at 01:22 PM
Place this code inside your object's script, or create a new one and add this code to it:
void Update () {
if (Camera.main.WorldToViewportPoint (transform.position).x > 0.5f) { // checks if it's on the right side.
// it's on the right side
Debug.Log ("right side of screen");
} else { // if not, then it must be on the left side
// it's on the left side
Debug.Log ("left side of screen");
}
// add all of the code to the Update() function
}
This is untested. Let me know if you have any problems.
Answer by YoungDeveloper · Jun 24, 2015 at 08:06 PM
Get screen coordinates from the gameobject using WorldToScreenPoint.
Transform target;
Vector2 screenPosition = Camera.main.WorldToScreenPoint(target.position);
Get screen middle point by dividing Screen.width.
Compare the horizontal coordinates, if value is larger it means its on the right side.
What code should I use to find object screen coordinates
Your answer
Follow this Question
Related Questions
problem with dead space kinesis module system 1 Answer
Endless Jumper Platform Spawning Script 0 Answers
Accessing postion of an object x seconds ago in c # 1 Answer
Object starts floating after being fixed to bones 0 Answers
How to instantiate and delete spheres based on incomming coordinates stream 2 Answers