I have a flycam script I am using to record trailer footage, and I am having difficulty getting full range of movement from it as it uses the mouses movement for camera movement, so when the mouse cursor reaches the edge of the monitor, it locks up, preventing me from moving the camera further.
As you can see from line 25 in the code below, I have tried to lock the cursor, but as the script uses that for movement, it just jitters around as it fights the system.
I am sure this has a very simple fix, but I am at a loss (coders block from staring at it for the last 2 hours).
Apologies for any spelling errors in the post, I am English but am on strong pain killers and accurately typing is a little difficul;t…
Any ideas on how I can fix this issuue?
using UnityEngine;
using System.Collections;
public class FlyCam : MonoBehaviour {
public float speed = 50.0f; // max speed of camera
public float sensitivity = 0.25f; // keep it from 0..1
public bool inverted = false;
private Vector3 lastMouse = new Vector3(255, 255, 255);
// smoothing
public bool smooth = true;
public float acceleration = 0.1f;
private float actSpeed = 0.0f; // keep it from 0 to 1
private Vector3 lastDir = new Vector3();
// Use this for initialization
void Start () {
Cursor.visible = false;
//Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate () {
// Mouse Look
lastMouse = Input.mousePosition - lastMouse;
if ( ! inverted ) lastMouse.y = -lastMouse.y;
lastMouse *= sensitivity;
lastMouse = new Vector3( transform.eulerAngles.x + lastMouse.y, transform.eulerAngles.y + lastMouse.x, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
// Movement of the camera
Vector3 dir = new Vector3(); // create (0,0,0)
if (Input.GetKey(KeyCode.W)) dir.z += 1.0f;
if (Input.GetKey(KeyCode.S)) dir.z -= 1.0f;
if (Input.GetKey(KeyCode.A)) dir.x -= 1.0f;
if (Input.GetKey(KeyCode.D)) dir.x += 1.0f;
dir.Normalize();
if (dir != Vector3.zero) {
// some movement
if (actSpeed < 1)
actSpeed += acceleration * Time.deltaTime * 40;
else
actSpeed = 1.0f;
lastDir = dir;
} else {
// should stop
if (actSpeed > 0)
actSpeed -= acceleration * Time.deltaTime * 20;
else
actSpeed = 0.0f;
}
if (smooth)
transform.Translate( lastDir * actSpeed * speed * Time.deltaTime );
else
transform.Translate ( dir * speed * Time.deltaTime );
}
void OnGUI() {
//GUILayout.Box ("actSpeed: " + actSpeed.ToString());
}
}