23 C
New York
Friday, July 4, 2025

How can I take advantage of offset to make the digicam rotate across the participant, however preserve the space from participant?


The script is hooked up to the Essential Digital camera.

On the high :

[Header("Camera Orbit")]
public bool orbitCamera = false;
public float rotationSpeed;
public Vector3 offset;

Then within the Begin()

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

And within the Replace()

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

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

How can I make that it’s going to not change the space between the digicam and the participant? Not that the digicam will likely be on the participant but when I begin the sport and the space between the digicam and the participant is 4 then preserve 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.

It is a screenshot displaying the digicam and the participant distance when operating the sport :

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;
rework.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 purple circle the participant to indicate how fats the digicam is from the participant.

What I am attempting to do the primary objective is to have the ability to use the mouse to rotate the digicam up down left proper and likewise to rotate the digicam across the participant.

Technically it is working however I do not need the digicam to be too removed from the participant with the offset half I need to preserve the space from the digicam as it’s earlier than operating the sport.

The complete script :

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

public class CameraController : MonoBehaviour
{
    [Header("Camera Transitions")]
    public bool startTransitions = false;
    public bool waitBeforeStart = false;
    public float transitionTimeToWait;
    public Remodel transitionTarget;
    public float movementSpeed;

    [Header("Follow/Look AT")]
    public Remodel observe;
    public Remodel lookAt;

    [Header("Camera Orbit")]
    public bool orbitCamera = false;
    public float rotationSpeed;

    [Header("Player")]
    public Remodel participant;
    public Vector3 offset;

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

        if (waitBeforeStart)
        {
            startTransitions = false;

            StartCoroutine(TimeToStartTransition());
        }
    }

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

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

            if (rework.place == transitionTarget.place)
            {
                rework.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