Friday, July 4, 2025
spot_img

How can I exploit offset to make the digital camera rotate across the participant, however hold the gap from participant?


The script is connected to the Principal Digicam.

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)
    {
        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 in addition 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’ll not change the gap between the digital camera and the participant? Not that the digital camera might be on the participant but when I begin the sport and the gap 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 gap radius could be very very far.

It is a screenshot exhibiting the digital camera and the participant distance when working the sport :

The camera distance from player when running the game

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 point out how fats the digital camera is from the participant.

What I am making an attempt to do the principle aim is to have the ability to use the mouse to rotate the digital camera up down left proper and in addition 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 gap 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("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)
        {
            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

- Advertisement -spot_img

Latest Articles