# RTS Camera move slow/fast

Hello everyone,
i currently have this code which works perfectly fine when it comes to move the camera

``````void Update ()
{

float mousePosX = Input.mousePosition.x;
float mousePosY = Input.mousePosition.y;

int Boundary = 5;
int scrollSpeed = 20;

if(mousePosX < Boundary)
{
transform.Translate(Vector3.right* - scrollSpeed * Time.deltaTime);
}

if(mousePosX >= Screen.width- Boundary)
{
transform.Translate (Vector3.right * scrollSpeed * Time.deltaTime);
}

if(mousePosY < Boundary)
{
transform.Translate (Vector3.forward * - scrollSpeed *Time.deltaTime);
}

if(mousePosY >= Screen.height - Boundary)
{
transform.Translate (Vector3.forward *scrollSpeed * Time.deltaTime);
}
}
``````

but i would like to make the screen when its farther from the screen edge (Boundary = 50), to move the camera slower and when the mouse touches the edge of the screen to move faster(Boundary = 10).

I know it could be done buy copy paste the whole code and changing the Boundary value.

But i was wondering if there is a more practical way of doing it. like in one code have the mouse been detected that is getting closer to the edge. and the closer to the edge the faster it will move the camera.

Most basic option would be to multiply speed by boundary minus position.

So you are saying the only way to succeed something like that is to copy paste the whole script 3x and just set *2 and *10 the boundary to succeed 3 different speeds?

No - what he is saying is come up with a formula you want to use to increase the speed.

Because I was bored…
You can use the below as a template… the formula I have used is simple:
Multiply the max speed you want by the inverse distance from edge of screen.
In this situation I have multiplied that by a base scroll speed factor (and capped it at max speed) to make it a bit smoother.

edit - Note this can be improved but is fully functional and just a quick idea of what Kelso was talking about so you can work on it to your own needs

``````	const int MOUSE_MOVEMENT_DISTANCE_FROM_SCREEN_EDGE = 100; // Bigger number means screen will start moving faster
const int MOUSE_MOVEMENT_MAX_SCROLL_SPEED = 20;//Increase this to increase max speed overall
const int BASE_SCROLLSPEED_FACTOR = 10; //Increase this to increase the rate of speed gain - 5-10 is a good range

void Update ()

{
float mousePosX = Input.mousePosition.x;
float mousePosY = Input.mousePosition.y;
int boundaryTrigger = MOUSE_MOVEMENT_DISTANCE_FROM_SCREEN_EDGE;
int scrollSpeed = MOUSE_MOVEMENT_MAX_SCROLL_SPEED;

if(mousePosX < boundaryTrigger)
{
scrollSpeed = (int)((BASE_SCROLLSPEED_FACTOR/mousePosX) * MOUSE_MOVEMENT_MAX_SCROLL_SPEED);
if ((scrollSpeed > MOUSE_MOVEMENT_MAX_SCROLL_SPEED)||(scrollSpeed < 0)) {
scrollSpeed = MOUSE_MOVEMENT_MAX_SCROLL_SPEED;
}

transform.Translate(Vector3.right* - scrollSpeed * Time.deltaTime);
}

if(mousePosX >= Screen.width- boundaryTrigger)
{
scrollSpeed = (int)((BASE_SCROLLSPEED_FACTOR/(Screen.width - mousePosX)) * MOUSE_MOVEMENT_MAX_SCROLL_SPEED);
if ((scrollSpeed > MOUSE_MOVEMENT_MAX_SCROLL_SPEED)||(scrollSpeed < 0)) {
scrollSpeed = MOUSE_MOVEMENT_MAX_SCROLL_SPEED;
}

transform.Translate (Vector3.right * scrollSpeed * Time.deltaTime);
}

if(mousePosY < boundaryTrigger)
{
scrollSpeed = (int)((BASE_SCROLLSPEED_FACTOR/mousePosY) * MOUSE_MOVEMENT_MAX_SCROLL_SPEED);
if ((scrollSpeed > MOUSE_MOVEMENT_MAX_SCROLL_SPEED)||(scrollSpeed < 0)) {
scrollSpeed = MOUSE_MOVEMENT_MAX_SCROLL_SPEED;
}

transform.Translate (Vector3.forward * - scrollSpeed *Time.deltaTime);
}

if(mousePosY >= Screen.height - boundaryTrigger)
{
scrollSpeed = (int)((BASE_SCROLLSPEED_FACTOR/(Screen.height - mousePosY)) * MOUSE_MOVEMENT_MAX_SCROLL_SPEED);
if ((scrollSpeed > MOUSE_MOVEMENT_MAX_SCROLL_SPEED)||(scrollSpeed < 0)) {
scrollSpeed = MOUSE_MOVEMENT_MAX_SCROLL_SPEED;
}

transform.Translate (Vector3.forward *scrollSpeed * Time.deltaTime);
}

}
``````

this works perfect. much better than the one i made. Thank you both
Much smoother.

No probs! Using the base idea of that it shouldn’t be too hard to implement whatever formula you want for speed of movement based on cursor position.

Good luck with the game!