- Home /
Prevent player from moving outside of camera view
I'm using a virtual joystick to move the player in my top-down 2d space shooter(with 3d rigibody and colliders) and i need to prevent the player from going off-screen, i've done some searching on google and found some interesting stuff but some answers were either not in c# or too vague or i failed to implement them. I need it to work for different screen sizes/resolutions so i think i'm going to need this:
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
and the code below is what i use to make the player move:
Vector3 moveVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"),0, CrossPlatformInputManager.GetAxis("Vertical"))* speed;
myBody.AddForce(moveVec);
The entire object needs to stay inside, can't have parts sticking out
Answer by meechan007 · Jan 09, 2016 at 01:59 PM
I am still getting used to unity but i think this may help. This what i would try to use. http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
How can i make a sprite move in the direction it is rotated? 1 Answer
Problem with iTween and instantiated path 0 Answers
Create bounded movement 1 Answer