2D Unity game: “Zoom” offset camera not working as needed

  Kiến thức lập trình

Example of current situation

I’m having an issue where I have a main character, and a camera that points to it. There’s a “zoom function” which doesn’t zoom IN or OUT, but shifts the camera position towards the mouse pointer by an offset I choose. Everything works fine, until I found this issue: Whenever I zoom in any direction, and try to go with the cursor near the main character, the camera orbits around it by the specified offset multiplier, and doesn’t allow the cursor to go nowhere near. I’m aware of why this happens, but I don’t know how to solve it. I want my cursor to freely go near the main character while the camera maintains the same offset and stays in place.

This is my code for the camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraSettings : MonoBehaviour
{
    CinemachineVirtualCamera cm;
    public GameObject mainChar;
    public GameObject cameraZoomTarget;
    public Vector3 offset;
    public Vector3 mousePos;
    public float offsetMultiplier = 5;
    //public float clampValue = 0.5f;
    void Start()
    {
        cm = GetComponent<CinemachineVirtualCamera>();
        offset = new Vector3(0, 0, -10);
        cameraZoomTarget.transform.position = mainChar.transform.position;
    }


    private void LateUpdate()
    {
        if (cm.Follow != null)
        {
            UpdateMousePosition();

            if (Input.GetMouseButton(1))
            {
                offset = (mousePos - mainChar.transform.position).normalized * offsetMultiplier;

                cameraZoomTarget.transform.position = mainChar.transform.position + offset;
            }

            if (!Input.GetMouseButton(1))
            {
                offset = new Vector3(0, 0, -10);
                cameraZoomTarget.transform.position = mainChar.transform.position;
            }
        }
    }


    private void UpdateMousePosition()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePos.z = 0;
    }

}

Sorry if I was causing confusion while asking this question, I’ll be happy to clarify it more if needed.

I tried using multiple cameras, which of course wasn’t the issue. In fact, the CameraZoomTarget is an empty game object that is always placed on top of the offset, and is meant to be the moving target of the camera.

I also tried using a method involving Math.Clamp() to set a minimum to the offset multiplier, so that the orbitational effect would be less, but still present.

(With cm.Follow != Null, I check every frame if the object followed by the Cinemachine camera isn’t null for whatever reason)

It’s difficult to understand exactly what is going on without seeing a video of it, but I would guess you would do this by editing the camera’s position, not the offset.

So when NOT following the mouse:

cameraZoomTarget.transform.position = mainChar.transform.position + offset;

But when you ARE following the mouse:

float zoomFactor = 0.5f; //Whatever looks good here where 0f = Following the Main Character completely, and 1f = Following the mouse completely
Vector3 positionA = mainChar.transform.position;
Vector3 positionB = mousePos;
cameraZoomTarget.transform.position = Vector3.Lerp(positionA, positionB, 0.5f) + offset;

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT