Почему я не могу изменить положение (я) движущихся объектов, а также положение (я) родителей? - PullRequest
0 голосов
/ 21 ноября 2018

Основная цель - изменить положение каждой лестницы 0,1,2,3,4 ... 12 и / или каждой дочерней лестницы.

Например, я не могу изменитьЛестница 0, а не ее дети.

Stairs

Этот сценарий прикреплен к лестнице 0: этот сценарий заставляет дочерние объекты перемещаться.

Если я отключу и не использую этот скрипт, я могу переместить Лестницу 0 и детей.Но когда скрипт включен, я не могу.

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

public class MoveObjects : MonoBehaviour
{
    public enum LineType
    {
        Curve, Straight
    }

    public List<GameObject> objectsToMove = new List<GameObject>();
    public AnimationCurve curve;
    public float stepsPerSecond = 1f;
    public bool changeDirection = false;
    //public LineType lineType;

    private Vector3 trackStart;
    private Vector3 trackEnd;
    private Vector3 horizontalTravel;
    private float verticalTravel;
    private float divisor;
    private float phase = 0f;

    // Use this for initialization
    public void Init()
    { // TO USE TAGS TO FIND THE STAIRS/STAIR BETWEEN SCRIPTS


        //lineType = LineType.Curve;

        if (curve == null)
        {
            curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
        }
        curve.preWrapMode = WrapMode.Clamp;
        curve.postWrapMode = WrapMode.Clamp;

        trackStart = objectsToMove[0].transform.position;
        int count = objectsToMove.Count;
        var span = objectsToMove[count - 1].transform.position - trackStart;

        divisor = 1f / count;
        horizontalTravel = (count + 1) * span * divisor;
        horizontalTravel.y = 0f;

        verticalTravel = span.y;
        trackEnd = trackStart + (count + 1) * span / count;
    }

    // Update is called once per frame
    void Update()
    {
        if (objectsToMove != null && objectsToMove.Count > 0 && curve != null)
        {
            AnimationCurve();
            /*curve = new AnimationCurve(curve.keys[0], curve.keys[1]);

            if (lineType == LineType.Straight)
            {
                StraightLineTrack();
            }

            else if (lineType == LineType.Curve)
            {
                AnimationCurve();
            }*/
        }
    }

    private void AnimationCurve()
    {
        phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);

        for (int i = 0; i < objectsToMove.Count; i++)
        {
            float t = Mathf.Repeat(phase + i * divisor, 1f);
            // Get the height of the curve at this step.
            float curveHeight = curve.Evaluate(t) * verticalTravel;

            if (changeDirection)
            {
                objectsToMove[i].transform.position = trackStart                // First step
                                  - horizontalTravel * t      // evenly spaced horizontal
                                  + curveHeight * Vector3.up; // curving vertical
            }
            else
            {
                objectsToMove[i].transform.position = trackStart                // First step
                                  + horizontalTravel * t      // evenly spaced horizontal
                                  + curveHeight * Vector3.up; // curving vertical
            }
        }
    }

    private void StraightLineTrack()
    {
        float divisor = 1f / objectsToMove.Count;

        // Compute the current phase of the escalator, 
        // from 0 (1st step at track start) to 1 (1st step at track end)
        phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);

        // Place each step a proportional distance along the track.
        for (int i = 0; i < objectsToMove.Count; i++)
        {
            float t = Mathf.Repeat(phase + i * divisor, 1f);
            objectsToMove[i].transform.position = Vector3.Lerp(trackStart, trackEnd, t);
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...