- Home /
Question by
TrivialRoo · Dec 27, 2013 at 02:53 PM ·
camerarotationquaternion
Camera rotation script logic error?
I believe I have a logic error in my script. I want to make the camera rotate, and I screwed up.
Here's the script;
using UnityEngine;
using System.Collections;
public class cameraController : MonoBehaviour {
public float dragSpeed;
private Vector3 dragOrigin;
private Quaternion rotateOrigin;
public Transform target;
void Start () {
dragSpeed = PlayerPrefs.GetFloat("dragSpeed", 2f);
}
// Camera movement
void Update () {
CameraDrag();
CameraRotation ();
if(Input.GetAxis ("Mouse ScrollWheel") < 0) {
if(Camera.main.fieldOfView <= 70) {
Camera.main.fieldOfView += 2f;
}
if(Camera.main.orthographicSize <= 20) {
Camera.main.orthographicSize += 0.5f;
}
}
if(Input.GetAxis("Mouse ScrollWheel") > 0) {
if(Camera.main.fieldOfView > 20) {
Camera.main.fieldOfView -= 2f;
}
if(Camera.main.orthographicSize >= 1) {
Camera.main.orthographicSize -= 0.5f;
}
}
}
void OnDestroy() {
PlayerPrefs.SetFloat("dragSpeed", dragSpeed);
}
void CameraDrag() {
if (Input.GetMouseButtonDown(2)) {
dragOrigin = Input.mousePosition;
}
else if(!Input.GetMouseButton (2)) {
return;
}
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
transform.Translate(move, Space.World);
}
void CameraRotation() {
if(Input.GetMouseButton (1)) {
/*float h = dragSpeed * Input.GetAxis ("Mouse X");
float v = dragSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);*/
rotateOrigin = Quaternion.identity;
}
else if(!Input.GetMouseButton (1)){
return;
}
Quaternion cameraRot = Camera.main.transform.rotation;
Quaternion rotate = new Quaternion(cameraRot.x, cameraRot.y, cameraRot.z, cameraRot.w);
}
}
Comment
In your CameraRotation()
you assign the current camera rotation to the local variable cameraRot
then effectively clone it and assign the clone to rotate
. At no point to actually rotate anything or assign any values back to the camera.
Oh, yeah. I changed some of it as well, because I realized that you needed a Vector3 Axis. But now I get funky rotation that spins you around and around and not very smooth at all....
`void CameraRotation() {
if(Input.Get$$anonymous$$ouseButton (1)) {
/*float h = dragSpeed * Input.GetAxis ("$$anonymous$$ouse X");
float v = dragSpeed * Input.GetAxis("$$anonymous$$ouse Y");
transform.Rotate(v, h, 0);*/
rotateOrigin = Quaternion.identity;
}
else if(!Input.Get$$anonymous$$ouseButton (1)){
return;
}
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
transform.Rotate (move, Space.World);
}
`