Hi, I am creating a simple 2D Space Shooter game and I am stuck at the point where the Object should limit itself to moving beyond the left and right edges of the screen.
I implemented the @Waz solution in one of the answers in Unity Answers, and it works fine if the object is not rigid. However, if it is applied to a rigid object, the object begins to flicker. Below is the code I used from @Waz
float speed = 0.1f;
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp01(viewPos.x);
viewPos.y = Mathf.Clamp01(viewPos.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
Here is the link where @Waz mentioned his code snippet:
http://answers.unity3d.com/questions/148790/detecting-the-edge-of-the-screen.html
Here is a link that says to use an alternative solution for a rigid body, but this code does not work for me:
http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html
I am not sure how to change the above code so that the object that I touch and does not move does not flicker. Any help would be great.
source
share