Sunday, June 12, 2022
HomeGame Developmentunity - Why the participant is making rotations nonstop?

unity – Why the participant is making rotations nonstop?


personal void Replace()
{
  targetAngles = remodel.eulerAngles + 180f * Vector3.up;
  remodel.eulerAngles = Vector3.Lerp(remodel.eulerAngles, targetAngles, 3f * Time.deltaTime);
}

I suppose as a result of it is making the rotating by 180 levels on a regular basis.
How am i able to make it that the participant will rotate solely as soon as by 180 levels with out utilizing coroutine ?

earlier than that i used within the Replace with a coroutine and a flag to begin the coroutine solely as soon as and it did rotate discover however with stuttering i assume the coroutine did not sync ok with the participant motion.

Whereas the participant is rotating i additionally make the participant begin shifting.

That is the coroutine i used earlier than however then when the participant begin shifting and rotating he was stuttering.

public IEnumerator DoRotate(float time)
    {
        var startRotation = remodel.rotation;
        var newRotation = Quaternion.AngleAxis(180, Vector3.up) * remodel.rotation;
        var progress = 0f;

        whereas (progress < 1)
        {
            progress += Time.deltaTime / time;
            remodel.rotation = Quaternion.Slerp(startRotation, newRotation, progress);

            yield return null;
        }

        remodel.rotation = newRotation;
    }

That is the complete script the rotating half is at strains 56-57 :
On the backside the coroutine i used to make the rotation however as a result of the stuttering i wish to attempt to do it with out coroutine to see if it’s going to repair the stuttering drawback.

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

public class DistanceCheck : MonoBehaviour
{
    public Remodel objectToRotatetowards;
    public float slowDownLerpDuration;
    public float speedUpLerpDuration;
    public float rotationSpeed;
    public GameObject descriptionTextImage;
    public TextMeshProUGUI textual content;
    public ThirdPersonUserControl thirdPersonUserControl;

    personal Animator anim;
    personal float timeElapsed = 0;
    personal float startValue = 1;
    personal float endValue = 0;
    personal float valueToLerp = 0;
    personal bool startRotating = false;
    personal bool slowOnBack = true;
    personal bool exited = false;
    personal Vector3 exitPosition;
    personal float distance;
    personal bool rotateTowards = false;
    personal bool rotateBackwards = false;
    personal bool rotateTowardsObject = false;
    personal Vector3 targetAngles;


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

    personal void FixedUpdate()
    {
        if (startRotating)
        {
            if (rotateTowards)
            {
                if (rotateTowardsObject)
                {
                    Vector3 targetDirection = objectToRotatetowards.place - remodel.place;
                    float singleStep = 3f * Time.deltaTime;
                    Vector3 newDirection = Vector3.RotateTowards(remodel.ahead, targetDirection, singleStep, 0.0f);
                    remodel.rotation = Quaternion.LookRotation(newDirection);
                }

                if (rotateBackwards)
                {
                    targetAngles = remodel.eulerAngles + 180f * Vector3.up;
                    remodel.eulerAngles = Vector3.Lerp(remodel.eulerAngles, targetAngles, 3f * Time.deltaTime);
                    //StartCoroutine(DoRotate(2f));
                    //rotateBackwards = false;
                }
            }
        }

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

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

    personal void OnTriggerExit(Collider different)
    {
        if (different.tag == "NoExit")
        {
            rotateTowardsObject = true;
            rotateBackwards = false;
            descriptionTextImage.SetActive(true);
            if (different.TryGetComponent(out ColliderInfo information))
            {
                textual content.textual content = information.onExitText;
                rotateTowards = information.rotateTowards;
            }
            RepositionPlayer();
        }
        else if (different.tag == "NoEntry")
        {
            OnPlayerRepositioned();
        }
    }

    personal void OnTriggerEnter(Collider different)
    {
        if (different.tag == "NoExit")
        {
            OnPlayerRepositioned();
        }
        else if (different.tag == "NoEntry")
        {
            rotateTowardsObject = false;
            rotateBackwards = true;
            descriptionTextImage.SetActive(true);
            if (different.TryGetComponent(out ColliderInfo information))
            {
                textual content.textual content = information.onEnterText;
                rotateTowards = information.rotateTowards;
            }
            RepositionPlayer();
        }
    }

    personal void RepositionPlayer()
    {
        exited = true;
        slowOnBack = true;
        exitPosition = remodel.place;
        thirdPersonUserControl.enabled = false;
        StartCoroutine(SlowDown());
    }

    personal void OnPlayerRepositioned()
    {
        exited = false;
        startRotating = false;
        textual content.textual content = "";
        descriptionTextImage.SetActive(false);
    }

    IEnumerator SlowDown()
    {
        timeElapsed = 0;

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

            yield return null;
        }

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

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

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

    IEnumerator SpeedUp()
    {
        timeElapsed = 0;

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

            yield return null;
        }
    }

    public IEnumerator DoRotate(float time)
    {
        var startRotation = remodel.rotation;
        var newRotation = Quaternion.AngleAxis(180, Vector3.up) * remodel.rotation;
        var progress = 0f;

        whereas (progress < 1)
        {
            progress += Time.deltaTime / time;
            remodel.rotation = Quaternion.Slerp(startRotation, newRotation, progress);

            yield return null;
        }

        remodel.rotation = newRotation;
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments