Hello, I’m a total beginner in scripting and today I wrote my first litle camera movement script, wich is movin the camera on the center point between the player and the cursor.
When you paste snippets of code into the forums be sure to use Code Tags.
Using your code as a base, something like this might do the trick:
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
midPoint = (Player.transform.position + mousePosition) / 2;
// get the vector between the current and target positions
Vector3 deltaPosition = (Vector2)midPoint - (Vector2)Player.transform.position;
// if that vector is longer than the max distance
if(deltaPosition.magnitude > maxDistance) {
// clamp it to max distance
deltaPosition = Vector3.ClampMagnitude(deltaPosition, maxDistance);
midPoint = Player.transform.position + deltaPosition;
}
midPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards(transform.position, midPoint, Time.deltaTime * Damping);
I didn’t test this code, so forgive me if there’s an error. You’ll need to define whichever variables you’re missing, or rename to your preference.