Question by
Sobe459 · Jun 09, 2021 at 09:12 PM ·
camerainputpositioningzoom
Camera Controller default position?
Hello all, I'm working on my camera controller, Its an edit of the mouse orbit controller. I am trying to mic RunesScapes camera behavior. It seems to be working other than when the game starts its at the target's position further in the the minZoom. Can anyone show me what I'm doing wrong? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target;
public float xSpeed = 240.0f;
public float ySpeed = 123.0f;
public int yMinLimit = -723;
public int yMaxLimit = 877;
public float x = 0.0f;
public float y = 0.0f;
public float currentZoom = 1f;
public float maxZoom = 3f;
public float minZoom = .3f;
public float zoomSensitivity = .7f;
float zoomSmoothV;
float targetZoom;
public void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
public void LateUpdate()
{
if (target)
{
x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
float scroll = Input.GetAxisRaw("Mouse ScrollWheel") * zoomSensitivity;
if (scroll != 0f)
{
targetZoom = Mathf.Clamp(targetZoom - scroll, minZoom, maxZoom);
}
currentZoom = Mathf.SmoothDamp(currentZoom, targetZoom, ref zoomSmoothV, .15f);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -currentZoom) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360.0f)
angle += 360.0f;
if (angle > 360.0f)
angle -= 360.0f;
return Mathf.Clamp(angle, min, max);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to move a camera smoothly to a fixed position and back using C#? 0 Answers
Problems with rotation after zoom (camera like Sketchfab) 0 Answers
Input.GetTouch(0).deltaPosition for rollout on orbit camera behaving strangly 0 Answers
Cinemachine Freelook Camera Movement with Joystick 3 Answers
Moving relative to camera 0 Answers