Ivakha
April 21, 2016, 5:57pm
1
Hello!
In my 2D game camera follows the cursor, and player stays visible. Here is my script:
Vector3 position = (target.position + camera.ScreenToWorldPoint(Input.mousePosition)) / 2f;
Vector3 destination = new Vector3(position.x, position.y, -10);
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
When i get my cursor on the edge of the screen (for example (1,1) in ViewPortPoint coordinates), the player is barely visible. I want to set maximum value for cursor’s position. For example (1,1) will be the same as (0.8, 0.8) so the whole player is always visible.
Thank you for your attanetion!
Replace Input.mousePosition with a new Vector3 that consists of Input.mousePosition with screen size calculations.
float maxScreenPoint = 0.8f;
Vector3 mousePos = Input.mousePosition * maxScreenPoint + new Vector3(Screen.width, Screen.height, 0f) * ((1f - maxScreenPoint) * 0.5f);
//Vector3 position = (target.position + GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition)) / 2f;
Vector3 position = (target.position + GetComponent<Camera>().ScreenToWorldPoint(mousePos)) / 2f;
Vector3 destination = new Vector3(position.x, position.y, -10);
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
DageLV
August 1, 2018, 7:38am
3
Soo… How do i make this script work? I copy/paste the code in unity and i just get compiler errors. Anyone knows how to make the script actually work?