- Home /
Camera follow mouse, remain inside bounds?
Hi, I'm making a 2D sidescroller game, and I'm trying to write a script that allows the camera to move in conjunction with the mouse, but only within the bounds. Imagine I have a "target" object in the center of the screen. When I move my mouse above the camera bound, the camera smoothly move to a higher spot, but with a limit.
I currently have the camera follow the mouse movement :
function Update () {
transform.position = Input.mousePosition;
}
But I can't figure out how to restrain the camera movement so that it has a limit to how far it can stretch from a "target". How can I do this in javascript? Thanks for all the help!
Answer by fafase · Apr 12, 2012 at 07:43 AM
Restrict the position of your camera to the bounds
if(Input.MousePosition.x>leftBound && Input.MousePosition.x<rightBound)
camera.position.x = Input.MousePosition.x;
else if(Input.mousePosition.x > rightBound)
camera.position.x = rightBound;
else if(Input.mousePosition.x<leftBound)
camera.position.x = leftBound;
Answer by makegoalken · Mar 04, 2020 at 08:15 AM
A very nice blog, I like the way you share very honestly and interestingly, through my blog I learned a lot of things run 3
Answer by navonwolf · Nov 12, 2020 at 09:30 AM
I think an article becomes special due to the way you present it most practically. io games skribbl io
Your answer
Follow this Question
Related Questions
Orbital cameras and dragging objects with mouse. 0 Answers
Character Runs Off Camera 2 Answers
2d camera help! 0 Answers
Fixed camera on rails. 2 Answers
trying to get an object to follow mouse on GUI click 1 Answer