dimanche 28 mars 2021

Fading UI text from object Checksphere and interact

I'm trying to come up with a generic rule that I can add to any GameObject. Items like tables, chairs etc that once collided and interacted fade in the white text, then after a few seconds the text fades out.

Ideally I'd be able to offset the position of this text with a Vector3 so I can position it around my character

At the moment I have, some code but the CrossFade.Alpha doesn't seem to work. this might or might not be the best way to go about this, I'm open to new ideas

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class Qwerty : MonoBehaviour
{

    public float Radius = 1f;
    public Text TextToUse;
    [SerializeField] private LayerMask playerMask;
    private bool isInSphere;


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

    // Update is called once per frame
    void Update()
    {
        isInSphere = Physics.CheckSphere(transform.position, Radius, playerMask);
        if (isInSphere && Input.GetKey(KeyCode.F) == true)
        {
            // Check if collision is detected and F is pressed
            isInSphere = true;
            Debug.Log("Hello");
            //Fade in over 2 seconds
            TextToUse.CrossFadeAlpha(1, 2f, false);
        }

        else if (!isInSphere && !Input.GetKey(KeyCode.F))

        {
            //Fade out on leave over 2 seconds
            isInSphere = false;
            TextToUse.CrossFadeAlpha(0, 2f, false);
        }

    }
}

Aucun commentaire:

Enregistrer un commentaire