- Home /
Issues with switching cameras
Heya,
I'm currently making a little script that allows me to switch between 2 camera's (well, actually just one, but I'm enabling and disabling the first- and third-person scripts.)
The scene starts with a first person view. When I scroll (zoom out) I get a third person view. The first transition is flawless, but when I try this a second time the camera does weird things (it starts flying around or it stays right above my character,...). I have tried numerous things, like placing the camera at the player's position. But nothing seems to work.
The following is my messed up script, hope you can figure it out...
public class CameraSwitch : MonoBehaviour {
public float Zoom = 0F;
public Transform Target;
private Vector3 PlayerPosition;
private bool ResetThirdPersonPos;
// Use this for initialization
void Start ()
{
PlayerPosition = (Vector3)GameObject.FindGameObjectWithTag("MainCamera").transform.position - (Vector3)GameObject.FindGameObjectWithTag("Player").transform.position;
}
// Update is called once per frame
void Update ()
{
ZoomFirstToThird();
}
void ZoomFirstToThird()
{
Zoom -= (Input.GetAxis("Mouse ScrollWheel"));
if (Zoom < 0)
{
Zoom = 0;
}
if (Zoom == 0)
{
GetComponent<MouseLook>().enabled = true;
GetComponent<SmoothFollow>().enabled = false;
if (ResetThirdPersonPos == false)
{
this.transform.Translate(PlayerPosition,GameObject.FindGameObjectWithTag("MainCamera").transform);
ResetThirdPersonPos = true;
}
}
if (Zoom > 0)
{
GetComponent<SmoothFollow>().enabled = true;
GetComponent<MouseLook>().enabled = false;
ResetThirdPersonPos = false;
}
}
EDIT: clarified the 'weird things' bit.
Answer by DaveA · Jun 03, 2011 at 08:07 PM
if (Zoom == 0) is dangerous for float vars. Use Mathf.Approximately http://unity3d.com/support/documentation/ScriptReference/Mathf.Approximately.html
Just guessing but you might need to reset zoom = 0.0 before the 'second time'
The scroll wheel uses + and - 0.1 only, so it shouldn't be a big problem.. But I'll add it anyways, thanks!
EDIT: get it now. That's not the problem..
Your answer
Follow this Question
Related Questions
switching turns between two game objects. 1 Answer
Camera switch after specific time 1 Answer
What's wrong with my weapon switching/swapping script? 0 Answers
Switch camera to cloned object. 1 Answer
Camera switching trouble 2 Answers