【发布时间】:2020-04-04 22:22:44
【问题描述】:
我在下面有一些代码,可以将对象沿曲线投影到给定位置,并且效果很好。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
public GameObject platform;
public Vector3 targetPos;
public float speed = 10;
public float arcHeight = 1;
Vector3 startPos;
GameObject line;
// Use this for initialization
void Start () {
startPos = transform.position;
targetPos = platform.transform.position;
targetPos.x -= 0.7f;
targetPos.y += 1.5f;
}
// Update is called once per frame
void Update () {
movePlayer();
}
void movePlayer()
{
// Compute the next position, with arc added in
float x0 = startPos.x;
float x1 = targetPos.x;
float dist = x1 - x0;
float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);
float baseY = Mathf.Lerp(startPos.y, targetPos.y, (nextX - x0) / dist);
float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist);
Vector3 nextPos = new Vector3(nextX, baseY + arc, transform.position.z);
transform.position = nextPos;
// Do something when we reach the target
if (nextPos == targetPos) Arrived();
}
void Arrived()
{
Destroy(gameObject);
}
}
我现在希望能够添加一条可见的曲线来显示对象的路径。我看过一些使用 LineRenderer 的示例,但我不确定如何将其合并到我移动对象的方式中。任何有关如何做到这一点的帮助将不胜感激。
【问题讨论】: