Good day community!
I got this code attached to a camera:
using UnityEngine;
public class CameraController: MonoBehaviour {
public Transform target;
public float distance = 5f;
public float xSpeed = 10f;
public float ySpeed = 10f;
public float scrollSpeed = 20f;
float yMinLimit = -80f;
float yMaxLimit = 80f;
float distanceMin = 5f;
float distanceMax = 50f;
float x = 0f;
float y = 0f;
float s = 0f;
float smoothTime = 0.2f;
float xSmooth = 0f;
float ySmooth = 0f;
float xVelocity = 0f;
float yVelocity = 0f;
float smoothDist = 0f;
float scrollVelocity = 0;
void Start () {
if (target.GetComponent<Rigidbody>())
{
target.GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate () {
if (target != null)
{
x += Input.GetAxis("Mouse X") * xSpeed;
y -= Input.GetAxis("Mouse Y") * ySpeed;
y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
distance = Mathf.Clamp(distance + Input.GetAxis("Mouse ScrollWheel") * -distance, distanceMin, distanceMax);
smoothDist = Mathf.SmoothDamp(smoothDist, distance, ref scrollVelocity, smoothTime);
transform.localRotation = Quaternion.Euler(ySmooth, xSmooth, 0);
transform.position = transform.rotation * new Vector3(0.0f, 0.0f, -smoothDist) + target.position;
}
}
}
What this does is that the camera can track the object, follow it and rotate arround it, this works perfect on a plane surface, I have managed to align the player to a sphere, and parenting the camera to the player, will offset the camera’s rotation, so the camera rotates arround the player and looks good, the problem is that the player have a ragdoll effect, so if I keep the camera parented to the player, the camera rotates with the player and it does not look good.
I would ask for help to align the camera to the vector from the center of the sphere to the player, so it can rotate along that axis without weird angles.