Okay so I am following a tutorial about how to make a city game camera controller, And I followed it to the letter but still can`t zoom or turn with the mouse.
error 1 zoom.
It won’t zoom at all both scrollwheel and r/f won’t work.
error 2 turn with camera.
It just turn 1f and stop.
I am new and is trying to learn so probably just missed something!
and if you have any recommendations tell me please =)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform cameraTransform;
public float normalSpeed;
public float fastSpeed;
public float movementSpeed;
public float movementTime;
public float rotationAmount;
Vector3 zoomAmout;
public Vector3 newPosition;
public Quaternion newRotation;
public Vector3 newZoomAmount;
public Vector3 dragStartPosition;
public Vector3 dragCurrentPosition;
public Vector3 rotateStartPos;
public Vector3 currentRotationPos;
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
newZoomAmount = cameraTransform.localPosition;
}
// Update is called once per frame
void Update()
{
HandleMouseInput();
HandleMovementInput();
}
void HandleMovementInput()
{
if(Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = fastSpeed;
}
else
{
movementSpeed = normalSpeed;
}
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
newPosition += (transform.forward * movementSpeed);
}
if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
newPosition += (transform.forward * -movementSpeed);
}
if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.LeftArrow))
{
newPosition += (transform.right * movementSpeed);
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.RightArrow))
{
newPosition += (transform.right * -movementSpeed);
}
if(Input.GetKey(KeyCode.Q))
{
newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);
}
if(Input.GetKey(KeyCode.E))
{
newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);
}
if(Input.GetKey(KeyCode.R))
{
newZoomAmount += zoomAmout;
}
if(Input.GetKey(KeyCode.F))
{
newZoomAmount -= zoomAmout;
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoomAmount, Time.deltaTime * movementTime);
}
void HandleMouseInput()
{
if(Input.mouseScrollDelta.y != 0)
{
newZoomAmount += Input.mouseScrollDelta.y * zoomAmout;
}
if(Input.GetMouseButtonDown(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if(plane.Raycast(ray, out entry))
{
dragStartPosition = ray.GetPoint(entry);
}
}
if(Input.GetMouseButton(0))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if(plane.Raycast(ray, out entry))
{
dragCurrentPosition = ray.GetPoint(entry);
newPosition = transform.position + dragStartPosition - dragCurrentPosition;
}
}
if(Input.GetMouseButtonDown(2))
{
rotateStartPos = Input.mousePosition;
}
if(Input.GetMouseButton(2))
{
currentRotationPos = Input.mousePosition;
Vector3 diffrence = rotateStartPos - currentRotationPos;
rotateStartPos = currentRotationPos;
newRotation = Quaternion.Euler(Vector3.up * (-diffrence.x/5));
}
}
}