using System.Collections; using System.Collections.Generic; using UnityEngine; public class ParaCurve : MonoBehaviour { public Transform startPos; public Transform endPos; public Transform heightPos; //public float height; public float time; private float acceleration; private float speedX;//速度水平x分量 private float speedZ;//速度水平z分量 private float speedY;//垂直方向分量 // Use this for initialization void Start() { float height = heightPos.position.y - startPos.position.y; float distanceX = endPos.position.x - startPos.position.x; float distanceZ = endPos.position.z - startPos.position.z; speedX = distanceX / time; speedZ = distanceZ / time; speedY = 4 * height / time; acceleration = speedY / (0.5f * time); transform.position = startPos.position; transform.rotation = Quaternion.LookRotation(new Vector3(speedX, speedY, speedZ),Vector3.up); } // Update is called once per frame void FixedUpdate() { if ((transform.position - endPos.position).magnitude > 0.1f) { speedY -= acceleration * Time.deltaTime; //transform.rotation = Quaternion.Euler(speedX, speedY, speedZ); transform.rotation = Quaternion.LookRotation(new Vector3(speedX, speedY, speedZ), Vector3.up); Debug.Log(transform.forward); Debug.LogWarning(new Vector3(speedX, speedY, speedZ).normalized); float speed = (new Vector3(speedX, speedY, speedZ)).magnitude; transform.Translate(Vector3.forward * speed * Time.deltaTime); //Vector3.forward使用局部坐标的Z方向 } } private void DestroySelf() { GameObject.Destroy(this); } }
给定初始位置,目标位置,高度,时间。
计算速度在xyz方向分量,然后通过LookRotation,箭头指向速度方向。
使用transform.Translate(Vector3.forward * speed * Time.deltaTime); 移动。
======
一般是使用LookAt看向一个点,使用LookRotation沿着一个向量看过去。
======