Как я могу использовать enum, чтобы решить, перемещать ли объект один раз или пинг-понг? - PullRequest
0 голосов
/ 21 декабря 2018

Это почти то, что я хотел.Единственная проблема сейчас заключается в том, что при переходе в состояние «Один раз» в середине, когда он находится в состоянии «Пингпонг», он не продолжается, поскольку, когда он просто движется, круг прыгает вниз.

Я хочу, чтобы, если в середине я изменил егоКак только он будет медленно плавно двигаться от его текущей высоты, сделайте часть и остановитесь в нижней части.Или, может быть, другой, но теперь он не работает, он просто делает круг, чтобы прыгнуть на дно, не зная почему.

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

[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    public enum CircleHeight
    {
        Center, Bottom, Top, None
    };

    public enum AnimateCircleStates
    {
        Once, Pingpong
    };

    public CircleHeight circleheight;
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 1.5f;
    [Range(1, 50)]
    public float yradius = 1.5f;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;
    public bool draw = false;
    public bool animateCircle = false;
    public float animationSpeed = 0.5f;
    public AnimateCircleStates animatecircle;

    private LineRenderer line;
    private Renderer renderer;
    private float Bottom;
    private float Top;
    private float Center = 0;
    private float t = 0f;
    private float tGoal;
    private bool animateOnce = false;
    private bool animatePingpong = false;

    void Start()
    {
        circleheight = CircleHeight.Center;
        //animatecircle = AnimateCircleStates.Once;

        line = gameObject.GetComponent<UnityEngine.LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
        renderer = gameObject.GetComponent<Renderer>();

        Bottom = transform.InverseTransformPoint(renderer.bounds.min).y + 0.1f;
        Top = transform.InverseTransformPoint(renderer.bounds.max).y + 0.1f;
    }

    void Update()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;

        if (draw)
        {
            line.enabled = true;
            CreatePoints();
        }
        else
        {
            line.enabled = false;
        }
    }

    void CreatePoints()
    {
        float x;
        float z;

        float angle = 20;

        switch (circleheight)
        {
            case CircleHeight.Center:
                float centerT = Mathf.InverseLerp(Bottom, Top, Center);

                // t=0f+centerT, tGoal=1f to go to top first then stop at top
                // t=0f+centerT, tGoal=2f to go to top first then stop at bottom
                // t=2f-centerT, tGoal=2f to go to bottom first then stop at bottom
                // t=2f-centerT, tGoal=3f to go to bottom first then stop at top

                t = 2f - centerT;
                tGoal = 3f;
                height = Center;
                circleheight = CircleHeight.None;
                break;
            case CircleHeight.Bottom:
                t = 0f;
                tGoal = 1f;
                height = Bottom;
                break;
            case CircleHeight.Top:
                t = 1f;
                tGoal = 2f;
                height = Top;
                break;
            case CircleHeight.None:
                break;
        }

        if (animateCircle)
            AnimateCircle();

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, height, z));

            angle += (360f / segments + 1);
        }
    }

    private void AnimateCircle()
    {
        switch (animatecircle)
        {
            case AnimateCircleStates.Once:
                animateOnce = true;
                break;
            case AnimateCircleStates.Pingpong:
                animatePingpong = true;
                break;
        }

        if (animateOnce)
        {
            // prevent t from exceeding tGoal
            if (t > tGoal)
            {
                t = tGoal;
            }

            // end animation when t reaches goal
            if (t == tGoal)
            {
                animateOnce = false;
            }

            // advance height according to t
            height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f));

            // advance height according to time & speed
            t += animationSpeed * Time.deltaTime;
        }

        if (animatePingpong)
        {
            // advance height according to t
            height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f));

            // advance height according to time & speed
            t += animationSpeed * Time.deltaTime;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...