- Home /
The question is answered, right answer was accepted
Orbital Camera Spazzing out uncontrollably..
Hey all, Im currently working on a 3rd person camera. Kind of like in WoW. Anyway, ive got no errors but as soon as i hit play, the camera spazzes out uncontrollably like it doesnt know where to go, and it shifts the position from what seems to be the min position allowance to the current position, every frame.
here is my code:
using UnityEngine;
using System.Collections;
public class Third_Person_Camera : MonoBehaviour {
public static Third_Person_Camera Instance;
public Transform TargetLookAt;
public float distanceCurr = 5f;
public float distanceMin = 3f;
public float distanceMax = 10f;
public float distanceSmooth = 0.05f;
public float xMouseSensitivity = 5f;
public float yMouseSensitivity = 5f;
public float mouseWheelSensitivity = 5f;
public float xSmooth = 0.05f;
public float ySmooth = 0.1f;
public float yMinLimit = -40f;
public float yMaxLimit = 80f;
private float mouseX = 0f;
private float mouseY = 0f;
private float velX = 0f;
private float velY = 0f;
private float velZ = 0f;
private float velDistance = 0f;
private float startDistance = 0f;
private Vector3 position = Vector3.zero;
private Vector3 desiredPosition = Vector3.zero;
private float desiredDistance = 0f;
void Awake () {
Instance = this;
}
void Start () {
distanceCurr = Mathf.Clamp (distanceCurr, distanceMin, distanceMax);
startDistance = distanceCurr;
Reset ();
}
void LateUpdate () {
if (TargetLookAt == null) {
return;
}
HandlePlayerInput ();
CalculateDesiredPosition ();
UpdatePosition ();
}
void HandlePlayerInput () {
var deadZone = 0.01f;
//this checks to see if the right mouse is down.
//later check to see if also not over GUI.
if (Input.GetMouseButtonDown (1)) {
mouseX += Input.GetAxis("Mouse X") * xMouseSensitivity;
mouseY -= Input.GetAxis("Mouse Y") * yMouseSensitivity;
}
//clamp or limit mouse rotation for Y-Axis.
mouseY = camHelper.clampAngle (mouseY, yMinLimit, yMaxLimit);
if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone || Input.GetAxis ("Mouse ScrollWheel") > deadZone) {
desiredDistance = Mathf.Clamp(distanceCurr -
Input.GetAxis ("Mouse ScrollWheel") *
mouseWheelSensitivity,
distanceMin, distanceMax);
}
}
void CalculateDesiredPosition () {
//evaluate the players distance.
distanceCurr = Mathf.SmoothDamp (distanceCurr, desiredDistance, ref velDistance, distanceSmooth);
//then calculate the desired position..
desiredPosition = CalculatePosition (mouseY, mouseX, distanceCurr);
}
Vector3 CalculatePosition (float rotationX, float rotationY, float distance) {
Vector3 direction = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}
void UpdatePosition () {
var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
transform.position = new Vector3 (posX, posY, posZ);
transform.LookAt (TargetLookAt);
}
public void Reset () {
mouseX = 0;
mouseY = 10;
distanceCurr = startDistance;
desiredDistance = distanceCurr;
}
public static void UseExistingOrCreateNewMainCamera () {
GameObject tempCamera;
GameObject targetLookAt;
Third_Person_Camera myCamera;
if (Camera.mainCamera != null) {
tempCamera = Camera.mainCamera.gameObject;
} else {
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent("Camera");
tempCamera.tag = ("MainCamera");
}
tempCamera.AddComponent ("Third_Person_Camera");
myCamera = tempCamera.GetComponent ("Third_Person_Camera") as Third_Person_Camera;
targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
if (targetLookAt == null) {
targetLookAt = new GameObject("targetLookAt");
targetLookAt.transform.position = Vector3.zero;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
}
the error has something to do with this:
I have isolated the issue to this:
var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
transform.position = new Vector3 (posX, posY, posZ);
dont say its because on posZ one of the variable params should be zSmooth, cos thats not right.
Answer by thornekey · Aug 12, 2014 at 01:19 AM
I have isolated the issue to this:
var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, xSmooth);
var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, ySmooth);
var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, xSmooth);
transform.position = new Vector3 (posX, posY, posZ);
Answer by DajBuzi · Aug 11, 2014 at 03:31 PM
Hello,
For such easy task i think that you have wrote to much code, try this one:
public class SphericalCam : MonoBehaviour {
public float MinDist, CurrentDist, MaxDist, TranslateSpeed, AngleH, AngleV;
public Transform Target;
public void Update(){
AngleH += Input.GetAxis("Mouse X");
AngleV -= Input.GetAxis("Mouse Y");
CurrentDist += Input.GetAxis("Mouse ScrollWheel");
}
public void LateUpdate(){
Vector3 tmp;
tmp.x = (Mathf.Cos(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.x;
tmp.z = (Mathf.Sin(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.z;
tmp.y = Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.y;
transform.position = Vector3.Slerp(transform.position, tmp, TranslateSpeed * Time.deltaTime);
transform.LookAt(Target);
}
}
That should run smoothly ;)
Regards, M.Rogalski
Follow this Question
Related Questions
Smooth Mouse Orbit 1 Answer
Rotate an object around another on input 0 Answers
Orbiting w/ Touch, Camera "snaps" 180 at north & south pole (webplayer link) 0 Answers
Android deltaPosition.y isn't working? 1 Answer
How to orbit a camera around an object with device(mobile) tilting/accelatarion? 0 Answers