I’m pretty new to unity but iv done some coding in the past. I have got some code for an orbital camera that sets to my target. The camera works…but… Only if I have the right mouse button down. For some reason its not updating and staying following the target after I let go. I’m probably missing something extremely basic here but if someone could give me a point in the right direction that would be awesome I just doesnt do anything unless I have the right mouse button press down: it doesnt even scroll unless I have the mouse button pressed.
public Transform playertarget;
private float x = 0.0f;
private float y = 0.0f;
private int mouseXSpeedMod = 5;
private int mouseYSpeedMod = 3;
public float maxVeiwDistance = 25; // how far the camera zooms out
public float minVeiwDistance = 3; // How close the camera zooms in
private float distance = 3; // Starting distance from player
private float desiredDistance; // used for calculations
private float correctDistance; // used for calculations
public int zoomRate = 30; // How fast the camera will zoom
public int lerpRate = 5;
public float playerTargetHight = 1.0f;
private float currentDistance;
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
currentDistance = distance;
desiredDistance = distance;
correctDistance = distance;
}
// Update is called once per frame
void Update()
{
}
void LateUpdate() //Update camera every frame
{
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
}
else if (Input.GetAxis("vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
float targetRotationAngle = playertarget.eulerAngles.y;
float cameraRotationAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpRate * Time.deltaTime);
}
y = ClampAngle(y, -50, 80);
Quaternion rotation = Quaternion.Euler(y, x, 0);
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); // calcualtes the distance between teh camera and the player
desiredDistance = Mathf.Clamp(desiredDistance, minVeiwDistance, maxVeiwDistance);
correctDistance = desiredDistance;
Vector3 position = playertarget.position - (rotation * Vector3.forward * desiredDistance);
RaycastHit collisionHit;
Vector3 playerTargetPosition = new Vector3(playertarget.position.x, playertarget.position.y + playerTargetHight, playertarget.position.z);
bool isCorrected = false;
if (Physics.Linecast(playerTargetPosition, position, out collisionHit))
{
position = collisionHit.point;
correctDistance = Vector3.Distance(playerTargetPosition, position);
isCorrected = true;
}
currentDistance = !isCorrected || correctDistance > currentDistance ? Mathf.Lerp(currentDistance, correctDistance, Time.deltaTime * zoomRate) : correctDistance;
position = playertarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -playerTargetHight, 0));
transform.rotation = rotation; // when you call transform within the script, it looks for the transform the script is atacthed too.
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle -= 360;
}
if (angle > 360)
{
angle += 360;
}
return Mathf.Clamp(angle, min, max);
}