- Home /
Question by
MostafaMostafa · Jun 21, 2016 at 07:43 AM ·
speedmouse positioncamera viewportmain cameraedges
Change speed value depends on the mouse position from the center
I change (screen edge movement) the position of the camera on mouse movement to a specified boundary, I want to reduce the speed of the camera according to the mouse position from the center. make the speed = 0 if the mouse is on the edge, and speed =10 while the mouse is at the center with respect to the in between values.
Comment
Answer by thor348k · Jun 21, 2016 at 09:41 AM
Here is a starting point:
void Update ()
{
// calculate speed based on camera relation to mouse
float speed = Vector2.Distance(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
// get the mouse postition with your camera z (so your camera doesn't change depth)
Vector3 mousePosition = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, Camera.main.transform.position.z);
// move the camera towards the mouse by calculated speed
Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, mousePosition, speed * Time.deltaTime);
}
Good luck! :)