Home > Back-end >  How do i make a toggle system in Unity
How do i make a toggle system in Unity

Time:12-28

I'm making a camera toggle system to toggle from Third Person mode to First person mode, however when i play test it in the editor it changes to Third person, however i cant toggle back to first person mode, I'm kind of new in Unity and i made this myself

Here is the code for the camera toggle script

{
public bool isTPMenabled = false;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.F5))
    {
        if (isTPMenabled == false)
        {
            if (isTPMenabled == true)
            {
                GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
                isTPMenabled = false;
                return;
            }
            GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x - 2, this.transform.position.y   2, this.transform.position.z);
            isTPMenabled = true;
            return;
        }
    }
    if (Input.GetKey(KeyCode.F4))
    {
        GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
    }

I used return; to try to terminate the script but that didn't work

My goal was if F5 was pressed 1 time it would go to Third Person Mode, but then if you press it again it would go back to First Person Mode

CodePudding user response:

Here is the revised code:

void Update()
{
    if (Input.GetKey(KeyCode.F5))
    {
        if (isTPMenabled == false)
        {
            GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x - 2, this.transform.position.y   2, this.transform.position.z);
            isTPMenabled = true;
        } else { // means "isTPMenabled == true"
            GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
            isTPMenabled = false;
        }
    }
    // Omitted the unrelated F4 codes
}

Such logic is much cleaner and straightforward.

CodePudding user response:

Adding to this answer in my eyes even cleaner would be

// If possible best already reference this via the Inspector
[SerializeField] private GameObject playerCamera;

private void Start ()
{
    // as a Fallback do this only ONCE
    if(!playerCamera) playerCamera = GameObject.Find("Player Camera");
}

void Update()
{
    // NOTE: You most probably do not want to toggle this EVERY FRAME
    // while the key stays pressed! You rather want to toggle ONCE per key press
    if (Input.GetKeyDown(KeyCode.F5))
    {
        // simply toggle the flag
        isTPMenabled = !isTPMenabled; 
        // since this is a single value assignment I'd prefer a ternary 
        playerCamera.transform.position = isTPMenabled ? transform.position   new Vector3(-2, 2, 0) : transform.position;
    }
}

Even easier it would become if you simply make the Camera a child of the player (if that's not the case anyway) and just operate on local space

playerCamera.transform.localPosition = isTPMenabled ? new Vector3(-2, 2, 0) : Vector3.zero;

Huge advantage of this is that it also takes rotation etc into account which the above does not yet.

  • Related