Created
November 15, 2022 04:17
-
-
Save Tunied/49e45abf0d809ec4837953f1ba9c61ce to your computer and use it in GitHub Desktop.
已知起点和终点进行抛物线移动
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sirenix.OdinInspector; | |
using UnityEngine; | |
public class PreGame_Mono_ParabolaMove : MonoBehaviour | |
{ | |
[OnValueChanged(nameof(DoRefresh))] | |
public Vector3 Distance = Vector3.one; | |
[OnValueChanged(nameof(DoRefresh)), Range(0, 1)] | |
public float DistancePercent = 0.5f; | |
[Range(0, 1f), OnValueChanged(nameof(DoRefresh))] | |
public float Percent = 0; | |
private void DoRefresh() | |
{ | |
var maxHigh = Vector3.Distance(Distance, Vector3.zero) * DistancePercent; | |
var pos = Parabola(Vector3.zero, Distance, maxHigh, Percent); | |
transform.position = pos; | |
} | |
/// <summary> | |
/// 3D 抛物线移动 | |
/// </summary> | |
/// <param name="start"></param> | |
/// <param name="end"></param> | |
/// <param name="height"></param> | |
/// <param name="t">百分比0~1</param> | |
/// <returns></returns> | |
public static Vector3 Parabola(Vector3 start, Vector3 end, float height, float t) | |
{ | |
float Func(float x) => -4 * height * x * x + 4 * height * x; | |
var mid = Vector3.Lerp(start, end, t); | |
return new Vector3(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t), mid.z); | |
} | |
/// <summary> | |
/// 2D 抛物线移动 | |
/// </summary> | |
/// <param name="start"></param> | |
/// <param name="end"></param> | |
/// <param name="height"></param> | |
/// <param name="t"></param> | |
/// <returns></returns> | |
public static Vector2 Parabola(Vector2 start, Vector2 end, float height, float t) | |
{ | |
float Func(float x) => -4 * height * x * x + 4 * height * x; | |
var mid = Vector2.Lerp(start, end, t); | |
return new Vector2(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment