0
\$\begingroup\$

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

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

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

I am using it in this script:

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

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

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

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

    private void FixedUpdate()
    {
        if (startRotating)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation,
    Quaternion.LookRotation(colliderInfo.rotateTowards.position - transform.position),
    rotationSpeed * Time.deltaTime);
        }

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

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

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "NoExit")
        {
            descriptionTextImage.SetActive(true);
            if (other.TryGetComponent(out ColliderInfo info))
            {
                text.text = info.onExitText;
            }
            RepositionPlayer();
        }
        else if (other.tag == "NoEntry")
        {
            OnPlayerRepositioned();
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "NoExit")
        {
            OnPlayerRepositioned();
        }
        else if (other.tag == "NoEntry")
        {
            descriptionTextImage.SetActive(true);
            if (other.TryGetComponent(out ColliderInfo info))
            {
                text.text = info.onEnterText;
            }
            RepositionPlayer();
        }
    }

    private void RepositionPlayer()
    {
        // Stuff that needs to happen to reposition the player
        exited = true;
        slowOnBack = true;
        exitPosition = transform.position;
        thirdPersonUserControl.enabled = false;
        StartCoroutine(SlowDown());
    }

    private void OnPlayerRepositioned()
    {
        // stuff you need to do to clear the "repositioning" status
        exited = false;
        startRotating = false;
        text.text = "";
        descriptionTextImage.SetActive(false);
    }

    IEnumerator SlowDown()
    {
        timeElapsed = 0;

        while (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            anim.SetFloat("Forward", 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;

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

            yield return null;
        }
    }
}

I want to use the variable rotateTowards in the ColliderInfo class for the rotation part, and the variable should be used to decide whether to rotate to face a gameobject's Transform, or to rotate the transform in the DistanceCheck script by degrees. For the latter case I need a Vector3 type variable. So I'm a bit confused how to use the ColliderInfo class for that case? Should I make another variable type Vectore3 in the ColliderInfo class? Then how I use it in the DistanceCheck with the rotation part?

This is a screenshot showing the colliders. The class ColliderInfo is attached to the small one and the bigger one:

Colliders

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "the variable should be used to decide whether to rotate to face a gameobject's Transform, or to rotate the transform in the DistanceCheck script by degrees". I'm lost on this part. Which variable should be used to decide how to rotate? In the latter case, how would you determine how many degrees to rotate? \$\endgroup\$
    – Kevin
    Jun 7 at 23:43
  • 1
    \$\begingroup\$ The script you posted contains a lot of functionality which doesn't appear to be relevant to the question. You might receive more answers if you shorten it to a minimal working example demonstrating the problem you face. \$\endgroup\$
    – Philipp
    Jun 8 at 7:20

0

You must log in to answer this question.

Browse other questions tagged .