Wednesday, June 8, 2022
HomeGame Developmentunity - How can i take advantage of a category for both...

unity – How can i take advantage of a category for both vector3 or remodel in the identical variable in a mono script?


I created this class, which is connected to an object with some colliders:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class ColliderInfo : MonoBehaviour
{
    [TextArea] public string onEnterText, onExitText;
    public Remodel rotateTowards;
}

I’m utilizing it on this script:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing TMPro;
utilizing UnityEngine;
utilizing UnityEngine.UI;
utilizing UnityStandardAssets.Characters.ThirdPerson;

public class DistanceCheck : MonoBehaviour
{
    public float lerpDuration;
    public float rotationSpeed;
    public GameObject descriptionTextImage;
    public TextMeshProUGUI textual content;
    public ThirdPersonUserControl thirdPersonUserControl;

    non-public Animator anim;
    non-public float timeElapsed = 0;
    non-public float startValue = 1;
    non-public float endValue = 0;
    non-public float valueToLerp = 0;
    non-public bool startRotating = false;
    non-public bool slowOnBack = true;
    non-public bool exited = false;
    non-public Vector3 exitPosition;
    non-public float distance;
    non-public ColliderInfo colliderInfo;

    void Begin()
    {
        anim = remodel.GetComponent<Animator>();
        colliderInfo = new ColliderInfo();
    }

    non-public void FixedUpdate()
    {
        if (startRotating)
        {
            remodel.rotation = Quaternion.RotateTowards(remodel.rotation,
    Quaternion.LookRotation(colliderInfo.rotateTowards.place - remodel.place),
    rotationSpeed * Time.deltaTime);
        }

        if (exitPosition != new Vector3(0, 0, 0) && slowOnBack)
        {
            distance = Vector3.Distance(remodel.place, exitPosition);
        }

        if (distance > 5 && slowOnBack)
        {
            slowOnBack = false;
            StartCoroutine(SlowDown());
        }
    }

    non-public void OnTriggerExit(Collider different)
    {
        if (different.tag == "NoExit")
        {
            descriptionTextImage.SetActive(true);
            if (different.TryGetComponent(out ColliderInfo information))
            {
                textual content.textual content = information.onExitText;
            }
            RepositionPlayer();
        }
        else if (different.tag == "NoEntry")
        {
            OnPlayerRepositioned();
        }
    }

    non-public void OnTriggerEnter(Collider different)
    {
        if (different.tag == "NoExit")
        {
            OnPlayerRepositioned();
        }
        else if (different.tag == "NoEntry")
        {
            descriptionTextImage.SetActive(true);
            if (different.TryGetComponent(out ColliderInfo information))
            {
                textual content.textual content = information.onEnterText;
            }
            RepositionPlayer();
        }
    }

    non-public void RepositionPlayer()
    {
        // Stuff that should occur to reposition the participant
        exited = true;
        slowOnBack = true;
        exitPosition = remodel.place;
        thirdPersonUserControl.enabled = false;
        StartCoroutine(SlowDown());
    }

    non-public void OnPlayerRepositioned()
    {
        // stuff you want to do to clear the "repositioning" standing
        exited = false;
        startRotating = false;
        textual content.textual content = "";
        descriptionTextImage.SetActive(false);
    }

    IEnumerator SlowDown()
    {
        timeElapsed = 0;

        whereas (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            anim.SetFloat("Ahead", valueToLerp);
            timeElapsed += Time.deltaTime;

            yield return null;
        }

        if (exited)
        {
            yield return new WaitForSeconds(3f);

            startRotating = true;
            StartCoroutine(SpeedUp());
        }

        if (slowOnBack == false)
        {
            thirdPersonUserControl.enabled = true;
        }
    }

    IEnumerator SpeedUp()
    {
        timeElapsed = 0;

        whereas (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
            anim.SetFloat("Ahead", valueToLerp);
            timeElapsed += Time.deltaTime;

            yield return null;
        }
    }
}

I need to use the variable rotateTowards within the ColliderInfo class for the rotation half, and the variable must be used to resolve whether or not to rotate to face a gameobject’s Remodel, or to rotate the remodel within the DistanceCheck script by levels. For the latter case I would like a Vector3 kind variable. So I am a bit confused the right way to use the ColliderInfo class for that case? Ought to I make one other variable kind Vectore3 within the ColliderInfo class? Then how I exploit it within the DistanceCheck with the rotation half?

It is a screenshot displaying the colliders. The category ColliderInfo is connected to the small one and the larger one:

Colliders

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments