- Home /
Camera script does not work for different scene
Hello. I copied a code for a 3D godlike view from a youtube tutorial for simulation games cameras. The code seemed to work well in the scene I wrote the code (i.e. all the settings for the camera were working as intended). However, when I try to copy the camera rig (which is an empty object including the camera and the camera settings codes such as moving forward with W key) into a different scene, it does not work at all. I am new to unity so I do not understand how can I implement a camera or any other code that I wrote from a scene to another. Thanks in advance.
Here's the original code:
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;
public Vector3 zoomAmount;
public Vector3 newPosition;
public Vector3 newZoom;
public Quaternion newRotation;
public Vector3 dragStartPosition;
public Vector3 dragCurrentPosition;
public Vector3 rotateStartPosition;
public Vector3 rotateCurrentPosition;
// 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()
{
HandleMouseInput();
HandleMovementInput();
}
void HandleMouseInput()
{
if(Input.mouseScrollDelta.y != 0)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
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))
{
rotateStartPosition = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
}
}
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.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
}
}
Your answer
Follow this Question
Related Questions
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
How to get the camera's size in world units 1 Answer
Change the FOV from "60" to "90" smoothly when pressing "W" (C#, first time posting, beginner) 2 Answers
Render texture on slim plane 1 Answer
gyroscope/compass camera for Unity 5 0 Answers