- Home /
Question by
Amberlewis012 · Dec 08, 2020 at 01:27 PM ·
camerainputcamera-movementcamera-look
How to convert a camera controller code to the "new" input system?
Hello, I'm experimenting with the "new" input system in Unity and I'm trying to make a camera controller using it but I don't know how it quite works. Is there a way to convert the following camera controller code (By the way, copied basically straight from this video ) to the new system? Also, sorry if this has already been answered but I can't find anything that works like this for some reason. Many thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour
{
public float movementSpeed;
public float movementTime;
public Vector3 newPosition;
public float normalSpeed;
public float fastSpeed;
public Quaternion newRotation;
public float rotationAmount;
public Transform cameraTransform;
public Vector3 zoomAmount;
public Vector3 newZoom;
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
newZoom = cameraTransform.localPosition;
}
// Update is called once per frame
void Update()
{
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.RightArrow))
{
newPosition += (transform.right * movementSpeed);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
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))
{
newZoom += zoomAmount;
}
if (Input.GetKey(KeyCode.F))
{
newZoom -= zoomAmount;
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
cameraTransform.transform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
Camera movement according to rotation and actual position RTS 1 Answer
How to create a camera follow setup. 1 Answer
Camera isn't following mouse in FPS 0 Answers
Minor Track Ball Issues 0 Answers
How to have free rotation camera? 1 Answer