0
我应该使用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);
}
}
}
在这种情况下,MoveElvator功能中的电梯正在向上移动。 现在我想让它达到高度50时开始向下移动,并在探测到/到达地面时停止。
,所以我说
if (distanceTravelled == distanceToTravel)
{
}
,但不知道如何使它向下移动,并停止获取到地面时。
这是C#不unityscript。顺便说一句,你没有得到答案,因为很难理解你的问题。 “现在我想让它达到50的高度时开始向下移动,并在探测到/到达地面时停止”....你在哪里检测它是否在地面上? – Programmer
顺便说一句,你的距离旅行是错误的,因为你增加了每一帧旅行的总距离。你应该只有:'distanceTravelled = Vector3.Distance(target.transform.position,startPositon);' – FINDarkside