简体   繁体   English

我如何上下移动物体?

[英]How can i move object up and back down?

Should i use StartCoroutine ? 我应该使用StartCoroutine吗?

using UnityEngine;
using System.Collections;
using System.Reflection;

public class DetectPlayer : MonoBehaviour {

    GameObject target;
    int counter = 0;
    public static bool touched = false;

    public float moveSpeed = 3.0f;
    public float smooth = 1f;
    private float distanceTravelled;
    private Vector3 startPositon;
    public float distanceToTravel = 50;

    private void Start()
    {
        startPositon = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
    }

    private void Update()
    {
        if (RaiseWalls.raised == true && touched == true)
        {
            MoveElevator();
            //touched = false;
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "ThirdPersonController") // "Platform"
        {
            Debug.Log("Touching Platform");
        }        
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "ThirdPersonController") // "OnTop Detector"
        {
            counter = 0;
            Debug.Log("On Top of Platform");
            target = GameObject.Find("Elevator");
            GameObject findGo = GameObject.Find("ThirdPersonController");
            GameObject findGo1 = GameObject.Find("Elevator");
            findGo.transform.parent = findGo1.transform;

            GameObject go = GameObject.Find("CubeToRaise");
            go.GetComponent<RaiseWalls>();
            Debug.Log("The button clicked, raising the wall");
            touched = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        GameObject findGo = GameObject.Find("ThirdPersonController");
        findGo.transform.parent = null;
    }

    void MoveElevator()
    {
        if (distanceTravelled == distanceToTravel)
        {

        }
        else
        {
            target.transform.localPosition += target.transform.up * Time.deltaTime * moveSpeed;
            distanceTravelled += Vector3.Distance(target.transform.position, startPositon);
        }
    }
}

In this case the elevator in the MoveElvator function is moving up. 在这种情况下,MoveElvator功能中的电梯正在向上移动。 Now i want to make that when it's getting to the height 50 to start moving back down and to stop when detecting/getting to the ground. 现在,我想在到达高度50时开始向下移动并在检测/着地时停止。

So i added 所以我加了

if (distanceTravelled == distanceToTravel)
            {

            }

But not sure how to make it move down and stop when getting to the ground. 但是不确定如何使其下降并在到达地面时停止。

You need to keep some kind of state whether the elevator is moving up, down or staying still. 无论电梯是向上,向下还是静止,您都需要保持某种状态。 You could use enums for that: 您可以为此使用枚举:

enum State{
   Idle,
   MovingUp,
   MovingDown
}

Now you just implement moving down and flip the direction when you are at the top or at the bottom. 现在,您只需实现上下移动并在顶部或底部翻转方向即可。

Here is my script that I use: 这是我使用的脚本:

float distance = 50;
float oneWayDuration;

void Start () {
    StartCoroutine(Mover(oneWayDuration, distance));
}

IEnumerator Mover(float oneWayDuration, float yDifference)
{
    Vector3 startPos = transform.position;
    Vector3 desirePos = new Vector3(startPos.x, startPos.y + 50 , startPos.z);
    float timeElapsed = 0f;

    while (timeElapsed < oneWayDuration)
    {
        timeElapsed += Time.deltaTime;
        transform.position = Vector3.Lerp(startPos, desirePos, timeElapsed / oneWayDuration);
        yield return new WaitForEndOfFrame();
    }
    while (timeElapsed < oneWayDuration)
    {
        timeElapsed += Time.deltaTime;
        transform.position = Vector3.Lerp(desirePos, startPos, timeElapsed / oneWayDuration);
        yield return new WaitForEndOfFrame();
    }
    //Just in case
    transform.position = startPos;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM