Thursday, July 3, 2025

How can I take advantage of offset to make the digital camera rotate across the participant, however hold the space from participant?

The script is connected to the Foremost Digital camera.

On the prime :

(Header("Digital camera Orbit"))
public bool orbitCamera = false;
public float rotationSpeed;
public Vector3 offset;

Then within the Begin()

non-public void Begin()
{
    offset = new Vector3(participant.place.x, participant.place.y, participant.place.z);
}

And within the Replace()

non-public void Replace()
{
    if (orbitCamera)
    {
        remodel.eulerAngles += rotationSpeed * new Vector3(-Enter.GetAxis("Mouse Y"), Enter.GetAxis("Mouse X"), 0);
 
        offset = Quaternion.AngleAxis(Enter.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
            remodel.place = participant.place + offset;
    }
 }

The issue is the offset or one thing e else place the digital camera too far in an enormous radius from the participant. I can rotate the digital camera across the participant with the mouse and likewise transfer the digital camera up down left proper throughout with the mouse the one thing with the half that rotates the digital camera across the participant make the digital camera place to be very removed from the participant.

How can I make that it’s going to not change the space between the digital camera and the participant? Not that the digital camera shall be on the participant but when I begin the sport and the space between the digital camera and the participant is 4 then hold this distance and use the mouse to rotate across the participant at a 4 distance radius.

Now the space radius could be very very far.

This can be a screenshot exhibiting the digital camera and the participant distance when working the sport :

How can I take advantage of offset to make the digital camera rotate across the participant, however hold the space from participant?

however then when it is attending to the offset half within the Replace this half :

offset = Quaternion.AngleAxis(Enter.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
remodel.place = participant.place + offset;

Then this occurs :

The camera is too far from the player the player is marked with red circle

I marked with crimson circle the participant to indicate how fats the digital camera is from the participant.

What I am making an attempt to do the principle purpose is to have the ability to use the mouse to rotate the digital camera up down left proper and likewise to rotate the digital camera across the participant.

Technically it is working however I do not need the digital camera to be too removed from the participant with the offset half I wish to hold the space from the digital camera as it’s earlier than working the sport.

The complete script :

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class CameraController : MonoBehaviour
{
    (Header("Digital camera Transitions"))
    public bool startTransitions = false;
    public bool waitBeforeStart = false;
    public float transitionTimeToWait;
    public Rework transitionTarget;
    public float movementSpeed;

    (Header("Observe/Look AT"))
    public Rework observe;
    public Rework lookAt;

    (Header("Digital camera Orbit"))
    public bool orbitCamera = false;
    public float rotationSpeed;

    (Header("Participant"))
    public Rework participant;
    public Vector3 offset;

    non-public void Begin()
    {
        offset = new Vector3(participant.place.x, participant.place.y, participant.place.z);

        if (waitBeforeStart)
        {
            startTransitions = false;

            StartCoroutine(TimeToStartTransition());
        }
    }

    non-public void Replace()
    {
        if (orbitCamera)
        {
            remodel.eulerAngles += rotationSpeed * new Vector3(-Enter.GetAxis("Mouse Y"), Enter.GetAxis("Mouse X"), 0);
            
            offset = Quaternion.AngleAxis(Enter.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
            remodel.place = participant.place + offset;
        }

        if (startTransitions && transitionTarget != null)
        {
            remodel.place = Vector3.MoveTowards(remodel.place, transitionTarget.place, movementSpeed * Time.deltaTime);

            if (remodel.place == transitionTarget.place)
            {
                remodel.gameObject.SetActive(false);
                transitionTarget.gameObject.SetActive(true);
            }
        }
    }

    IEnumerator TimeToStartTransition()
    {
        yield return new WaitForSeconds(transitionTimeToWait);

        startTransitions = true;
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles