I have a 1st person free flying camera. It can look around and move forward/backward/strafe.
When I hit a collider, the camera stops and does not go through the collider, but then, when I stop moving the camera around, it will continue to move slowly in a direction with no user input.
If I move around and look around with the camera and don't hit a collider, once I stop moving/looking the camera sits still and works perfect. It just gets messed up when I hit a collider then want to leave it looking at something.
This happens when using xbox controls and with mouse/keyboard.
The script for the camera is below.
Help!
using UnityEngine;
using System.Collections;
public class SmoothFlyCamera : MonoBehaviour
{
#region | [ Fields ] |
public float mainSpeed = 100.0f; //regular speed
public float shiftAdd = 250.0f; //multiplied by how long shift is held. Basically running
public float maxShift = 1000.0f; //Maximum speed when holdin gshift
public float camSens = 0.25f; //How sensitive it with mouse
private Vector3 lastMouse = new Vector3 (255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;
#endregion
#region | [ Properties ] |
public bool IsActive { get; set; }
#endregion
#region | Methods [ private ] |
private Vector3 GetBaseInput ()
{ //returns the basic values, if it's 0 than it's not active.
var v = Input.GetAxisRaw ("Vertical"); // > 0 ? Input.GetAxisRaw ("Vertical") : Input.GetAxisRaw ("Joy5Axis"));
var h = Input.GetAxisRaw ("Horizontal"); // > 0 ? Input.GetAxisRaw ("Horizontal") : Input.GetAxisRaw ("Joy4Axis"));
Debug.Log (" V = " + v + " h " + h);
Vector3 p_Velocity = Vector3.zero;
p_Velocity += new Vector3 (h, 0f, v);
/*
if (Input.GetKey (KeyCode.W)) {
p_Velocity += new Vector3 (0, 0, 1);
if (Input.GetKey (KeyCode.S)) {
p_Velocity += new Vector3 (0, 0, -1);
}
if (Input.GetKey (KeyCode.A)) {
p_Velocity += new Vector3 (-1, 0, 0);
}
if (Input.GetKey (KeyCode.D)) {
p_Velocity += new Vector3 (1, 0, 0);
}
*/
return p_Velocity;
}
#endregion
float distance = 5.0f;
float maxDistance = 20f;
float minDistance = 2.5f;
float xSpeed = 250.0f;
float ySpeed = 120.0f;
float angleX,angleY;
float yMinLimit = -20;
float yMaxLimit = 80;
private float x = 0.0f;
private float y = 0.0f;
#region | [ call backs ] |
void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
IsActive = false;
}
void FixedUpdate ()
{
if (IsActive) {
Debug.Log(" VV : " + Input.mousePosition);
// lastMouse = Input.mousePosition - lastMouse;
// lastMouse = new Vector3 (-lastMouse.y * camSens, lastMouse.x * camSens, 0);
// lastMouse = new Vector3 (transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
x += Input.GetAxis("Joy4Axis") * xSpeed * 0.02f;
y -= Input.GetAxis("Joy5Axis") * ySpeed * -0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0f);
transform.rotation = rotation;
//angleX = 60*Input.GetAxisRaw ("Horizontal");
//angleY = 60*Input.GetAxisRaw ("Vertical");
// transform.eulerAngles = //lastMouse;
// lastMouse = Input.mousePosition;
//Mouse & camera angle done.
// transform.eulerAngles =new Vector3(angleY, angleX, 0);
//Keyboard commands
float f = 0.0f;
var p = GetBaseInput ();
if (Input.GetKey (KeyCode.LeftShift)) {
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp (p.x, -maxShift, maxShift);
p.y = Mathf.Clamp (p.y, -maxShift, maxShift);
p.z = Mathf.Clamp (p.z, -maxShift, maxShift);
} else {
totalRun = Mathf.Clamp (totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
if (Input.GetKey (KeyCode.Space)) { //If player wants to move on X and Z axis only
f = transform.position.y;
transform.Translate (p);
transform.position = new Vector3 (transform.position.x, f, transform.position.z);
} else {
transform.Translate (p);
}
/*
RaycastHit hit;
if (Physics.Raycast (this.transform.position, transform.TransformDirection(Vector3.forward), out hit, 5f)) {
if (hit.collider.gameObject.tag.Equals ("CamStop")) {
Debug.Log (" STOP MC ");
return;
}
}
*/
}
}
public float ClampAngle (float angle,float min ,float max ) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
#endregion
}
↧