Hi everyone!
So I wrote this script myself and I’m having some minor issues with it. The objective of this script is to toggle between a first and third person camera I have setup on my player. It works! However i’m having an issue, as you can see in the code I have set logs to show when i’m switching. When I press “f5” (the default I have setup to run the script) It prints the lines twice… Implying that it is running twice. When watching it in the game scene, I can further confirm this as sometimes it switches on the first try, Sometimes it does not.
What I mean is I press “F5” and sometimes it goes to first person, then third person, then back to first person where it stays. It look rather weird. Other times it does the same pattern in a different order… leaving me in third person (third, first, third)
Im not sure what I did wrong… Is there anyone to make it more consistent? and also have it print the log once? Thanks for your replies in advance.
using UnityEngine;
using System.Collections;
public class ToggleView : MonoBehaviour {
public GameObject firstPersonCamera;
public GameObject thirdPersonCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate ()
{
if (Input.GetAxis("ToggleView") != 0)
{
if(firstPersonCamera.activeSelf == true)
{
firstPersonCamera.SetActive(false);
thirdPersonCamera.SetActive(true);
Debug.Log("Switched to Third Person");
}
else
{
firstPersonCamera.SetActive(true);
thirdPersonCamera.SetActive(false);
Debug.Log ("Switched to First Person");
}
}
}
}
=======================EDIT====================
I Actually made a Derp move… Its not an Axis haha its a button. I suspect it is because Axis smooths how the motion so it runs it a few times. While get Button just does the go and stops. Thank you MD_Reptile for your response!
I will post the working code if anyone wants it for their games.
using UnityEngine;
using System.Collections;
public class ToggleView : MonoBehaviour {
public GameObject firstPersonCamera;
public GameObject thirdPersonCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate ()
{
if (Input.GetButtonDown("ToggleView")) //Changed this from "GetAxis" to "GetButtonDown" This has solved the "jumpy issue"
{
if(firstPersonCamera.activeSelf == true)
{
firstPersonCamera.SetActive(false);
thirdPersonCamera.SetActive(true);
Debug.Log("Switched to Third Person");
}
else
{
firstPersonCamera.SetActive(true);
thirdPersonCamera.SetActive(false);
Debug.Log ("Switched to First Person");
}
}
}
}
=======================EDIT==========================
To get this working for someone else, all you have to do is drag the script on your gameobject (I chose my player) The make two Cameras, One in first person and one in third person. Then disable one. (I disabled the first person, so by default you are in third person) Finally drag the first and third person camera to their respective slot in the inspector. Make sure you go to input and make a new entry called “ToggleView” with your desired button. (I chose f5)