Move camera a bit depending on the mouse position

Hi folks!

I want to make my camera move a bit, where the mouse is in the screen like this, but not doing it soon well to be honest. If anyone could help it would mean a lot!

Sorry for bad paint job:

You want the camera to focus on a midpoint between the player and the mouse position.

focusPoint = (player.position + mousePosition) / 2;

I’m willing to help more, but I’d need more info (is the project 2D or 3D, do you need help getting the mouse position, etc.)

Its in 3d space and I want the object to be just like you said, in the mid point of those 2, The Mouse and the Player.

Also I do not want the camera, to ever escape from the player

Maybe you can calculate distance between the mouse and the camera and then use it as a multiplier to restrict the movement of the camera.

Try this in a monobehaviour (untested):

    public Transform cam;
    public Transform target1;
    public Transform target2;
    public float height = 2f;
    public float maxOffset = 5f;

    void Update () {
        float dist = Vector3.Distance(target1.position, target2.position);
        float distPercent = Mathf.Clamp01(dist / maxOffset);
        Vector3 delta = (target2.position - target1.position);
        Vector3 maxDelta = Vector3.Normalize(delta) * maxOffset;
        Vector3 finalDelta = Vector3.Lerp(delta, maxDelta, distPercent);
        cam.position = target1.position + finalDelta*0.5f + Vector3.up * height;
    }

Target1 would be the player, Target2 would be the mouse. Height defines the height of the camera. Use maxOffset to restrict the camera movement so target1 would never be out of sight.

1 Like

Sorry for a late reply was out a bit.

I’ll give it a shoot, and tell you if it works. Thanks a bunch!