- Home /
How can I move the camera when the mouse reaches the edge of the screen?
Hey just tell me what I need to do. I need some guidance ahhaha sorry im not the best programmer. Thanks
Answer by Chronos-L · Mar 27, 2013 at 06:47 AM
Try this function.
Vector2 MouseScreenEdge( int margin ) {
//Margin is calculated in px from the edge of the screen
Vector2 half = new Vector2( Screen.width/2, Screen.height/2 );
//If mouse is dead center, (x,y) would be (0,0)
float x = Input.mousePosition.x - half.x;
float y = Input.mousePosition.y - half.y;
//If x is not within the edge margin, then x is 0;
//In another word, not close to the edge
if( Mathf.abs(x) > half.x-margin ) {
x += (half.x-margin) * ( x < 0 )? 1 : -1;
}
else {
x = 0f;
}
if( Mathf.abs(y) > half.y-margin ) {
y += (half.y-margin) * ( y < 0 )? 1 : -1;
}
else {
y = 0f;
}
return new Vector2( x, y );
}
You define a margin of the edge of the screen, then this function will return a measurement on how close is the mouse to the edge.
For example, margin = 20:
Vector2 mouseEdge = MouseScreenEdge( 20 );
If mouse.Edge.x
is 0 (you need to use Mathf.Approximately()), then you have not touched the left or right side of the mouse. If mouse.Edge.x
is about 5, then your mouse is 15px away from the right side of the screen. If mouse.Edge.x
is about -20, then your mouse is on the leftmost side of your screen. The same goes for the y-axis.
From the Vector2
returned by this function, you can do something like this:
void Update() {
Vector2 mouseEdge = MouseScreenEdge( 20 );
if( !( Mathf.Approximately( mouseEdge.x, 0f) ) {
//Move your camera depending on the sign of mouse.Edge.x
if( mouseEdge.x < 0 ) {
//Move Left
}
else {
//Move right
}
}
}
Code is not tested, but I checked the calculation twice; so I am quite confident that this will work.
Another way to use this script is that if you use a Vector2 as margin, and then you insert the half screen-size a margin. The vector2 returned by the function will indicate which quadrant the mouse is at.
THAN$$anonymous$$S! It seemed that you put the most effort in trying to explain this so I gave you best answer. Thanks!
You can modify the function to use Vector2 as parameters if you need different margin on the left-right and top-bottom.
I am a very lazy fella, so I always try to make my function/code as reusable as possible; so I can re-adapt it to another game without hassle.
I am glad that I could help.
This is a great solution as the value you get also tells you how FAR from the edge the pointer actually is. It makes it easy to move the camera faster the closer to the edge it is. Thanks!
The code almost works, just 4 small fixes. $$anonymous$$athf.abs is actually $$anonymous$$athf.Abs (lines 12 and 19) and both ( x < 0 )? 1 : -1 (line 13) and ( y < 0 )? 1 : -1 (line 20) need to go in parenthesis.
Answer by nuverian · Mar 27, 2013 at 06:19 AM
You can translate the camera's transform on it's .x axis by Input.GetAxis("Mouse X") (which return a value from -1 to +1. And do this only if the Input.mousePosition.x is at the screen edge Screen.width So:
var camTransform:Transform;
var speed:float;
if (Input.mousePosition.x == Screen.width){
camTransform.position.x += Input.GetAxis("Mouse X") * Time.deltaTime * speed;
}
if (Input.mousePosition.x == 0){
camTransform.position.x -= Input.GetAxis("Mouse X") * Time.deltaTime * speed;
}
Answer by Seth-Bergman · Mar 27, 2013 at 06:26 AM
Well, not much point in explaining then
var sensitivity = 10;
function Update(){
if(Input.mousePosition.x < sensitivity)
transform.Rotate(-Vector3.up * Time.deltaTime * Mathf.Abs(sensitivity -Input.mousePosition.x);
else if(Input.mousePosition.x > Screen.width - sensitivity)
transform.Rotate(Vector3.up * Time.deltaTime * Mathf.Abs(Screen.width -Input.mousePosition.x);
}
add this javascript to your camera (untested..)