0
\$\begingroup\$

With only once, I mean once false and once true. The reason I want to enable and disable the script is that I don't want to disable the player controller and then after some point re-enable that controller again.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class FadeScript : MonoBehaviour
{
    public FirstPersonController fpc;
    public float fadeDuration = 5;
    public float speed;

    private Material material;
    private float targetAlpha = 0;
    private float lerpParam;
    private float startAlpha = 1;

    void Start()
    {
        material = GetComponent<Renderer>().material;
        SetMaterialAlpha(1);

        fpc.enabled = false;
    }

    void Update()
    {
        lerpParam += Time.deltaTime;

        float alpha = Mathf.Lerp(startAlpha, targetAlpha, lerpParam / fadeDuration);
        SetMaterialAlpha(alpha);

        if (alpha == 0)
        {
            fpc.enabled = true;
            var firstpc = fpc.GetComponent<FirstPersonController>();

            if (fpc.transform.rotation != Quaternion.Euler(0, 0, 0))
            {
                firstpc.enabled = false;
                fpc.transform.rotation = Quaternion.Slerp(fpc.transform.rotation, Quaternion.Euler(0, 0, 0), speed * Time.deltaTime);
            }
            if (fpc.transform.rotation == Quaternion.Euler(0,0,0))
            {
                firstpc.enabled = true;
            }
        }
    }

    public void FadeTo(float alpha, float duration)
    {
        startAlpha = material.color.a;
        targetAlpha = alpha;
        fadeDuration = duration;
        lerpParam = 0;
    }

    private void SetMaterialAlpha(float alpha)
    {
        Color color = material.color;
        color.a = alpha;
        material.color = color;
    }
}

This script is not attached to the FirstPersonController. That is why I added the global variable fpc.

I'm getting the script that is already attached to the fpc. I called it firstpc and the script name is: FirstPersonController

FirstPersonController gives the player a control over the fpc with the mouse camera. I want to disable the script until fpc.transform.rotation == Quaternion.Euler(0,0,0) and then enabled the script again to use.

The problem is that it's all the time getting over and over again to the line:

firstpc.enabled = false;

Then it's getting also to the line:

firstpc.enabled = true;

But since it's getting to the false all the time it's stuck between both states.

fpc

The script here in my question is attached to the Plane in the hierarchy. And the script I want to enable/disable is attached to the FPSController.

In my script that is attached to the Plane, fpc is the FPSController and firstpc is the script FirstPersonController that attached to the fpc.

\$\endgroup\$
1
\$\begingroup\$

If the engine uses global variables, start the script off with a conditional statement that checks for whether the statement that uses Boolean is true. If so, then use :

If (Bool1 == true) {
(have statements to be executed here.)
}
\$\endgroup\$
0
\$\begingroup\$

Solved it using a bool helper: rotated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class FadeScript : MonoBehaviour
{
    public FirstPersonController fpc;
    public float fadeDuration = 5;
    public float speed;

    private Material material;
    private float targetAlpha = 0;
    private float lerpParam;
    private float startAlpha = 1;
    private bool rotated = false;

    void Start()
    {
        material = GetComponent<Renderer>().material;
        SetMaterialAlpha(1);

        fpc.enabled = false;
    }

    void Update()
    {
        lerpParam += Time.deltaTime;

        float alpha = Mathf.Lerp(startAlpha, targetAlpha, lerpParam / fadeDuration);
        SetMaterialAlpha(alpha);

        if (alpha == 0)
        {
            fpc.enabled = true;

            if (rotated == false)
            {
                fpc.GetComponent<FirstPersonController>().enabled = false;
                fpc.transform.localRotation = Quaternion.Slerp(fpc.transform.rotation, Quaternion.Euler(0, 0, 0), speed * Time.deltaTime);
            }

            if (fpc.transform.localRotation == Quaternion.Euler(0,0,0))
            {
                fpc.GetComponent<FirstPersonController>().enabled = true;
                rotated = true;
            }
        }
    }

    public void FadeTo(float alpha, float duration)
    {
        startAlpha = material.color.a;
        targetAlpha = alpha;
        fadeDuration = duration;
        lerpParam = 0;
    }

    private void SetMaterialAlpha(float alpha)
    {
        Color color = material.color;
        color.a = alpha;
        material.color = color;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.