The script is attached to the Main Camera.
At the top :
[Header("Camera Orbit")]
public bool orbitCamera = false;
public float rotationSpeed;
public Vector3 offset;
Then in the Start()
private void Start()
{
offset = new Vector3(player.position.x, player.position.y, player.position.z);
}
And in the Update()
private void Update()
{
if (orbitCamera)
{
transform.eulerAngles += rotationSpeed * new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
}
}
The problem is the offset or something e else position the camera too far in a big radius from the player. I can rotate the camera around the player with the mouse and also move the camera up down left right all around with the mouse the something with the part that rotates the camera around the player make the camera position to be very far from the player.
How can I make that it will not change the distance between the camera and the player? Not that the camera will be on the player but if I start the game and the distance between the camera and the player is 4 then keep this distance and use the mouse to rotate around the player at a 4 distance radius.
Now the distance radius is very very far.
This is a screenshot showing the camera and the player distance when running the game :
but then when it's getting to the offset part in the Update this part :
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
Then this happens :
I marked with red circle the player to show how fat the camera is from the player.
What I'm trying to do the main goal is to be able to use the mouse to rotate the camera up down left right and also to rotate the camera around the player.
Technically it's working but I don't want the camera to be too far from the player with the offset part I want to keep the distance from the camera as it is before running the game.
The full script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[Header("Camera Transitions")]
public bool startTransitions = false;
public bool waitBeforeStart = false;
public float transitionTimeToWait;
public Transform transitionTarget;
public float movementSpeed;
[Header("Follow/Look AT")]
public Transform follow;
public Transform lookAt;
[Header("Camera Orbit")]
public bool orbitCamera = false;
public float rotationSpeed;
[Header("Player")]
public Transform player;
public Vector3 offset;
private void Start()
{
offset = new Vector3(player.position.x, player.position.y, player.position.z);
if (waitBeforeStart)
{
startTransitions = false;
StartCoroutine(TimeToStartTransition());
}
}
private void Update()
{
if (orbitCamera)
{
transform.eulerAngles += rotationSpeed * new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotationSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
}
if (startTransitions && transitionTarget != null)
{
transform.position = Vector3.MoveTowards(transform.position, transitionTarget.position, movementSpeed * Time.deltaTime);
if (transform.position == transitionTarget.position)
{
transform.gameObject.SetActive(false);
transitionTarget.gameObject.SetActive(true);
}
}
}
IEnumerator TimeToStartTransition()
{
yield return new WaitForSeconds(transitionTimeToWait);
startTransitions = true;
}
}
transform.eulerAngles +=
to your list of "code to never write again". Accumulating Euler angles with a round-trip conversion to a quaternion and back between each increment will almost certainly not behave the way you expect. This has caused trouble for other devs in the past that we've had to clear up here. \$\endgroup\$transform.eulerAngles +=
at all. Not twice, not once. Never. \$\endgroup\$