有没有办法使SetPosition的LineRenderer更平滑。我正在制作一个2D游戏,我制作了一个变色龙舌头,它从嘴里弹出来,然后又回来了,但这让动画变得非常快,有没有办法让它更慢、更流畅?
我的问题是,是否有一种方法来平滑一个衬垫的位置?就像我剧本里写的一样。
EdgeCollider2D edgeCollider;
LineRenderer myLine;
public Transform pointOne;
public Transform pointfinalZero;
public Transform pointfinal;
public bool isTongue;
void Start()
{
edgeCollider = this.GetComponent<EdgeCollider2D>();
myLine = this.GetComponent<LineRenderer>();
}
void Update()
{
SetEdgeCollider(myLine);
myLine.SetPosition(0, pointOne.position);
if(isTongue)
{
myLine.SetPosition(1, pointfinal.position);
}
if(!isTongue)
{
myLine.SetPosition(1, pointfinalZero.position);
}
}
void SetEdgeCollider(LineRenderer lineRenderer)
{
List<Vector2> edges = new List<Vector2>();
for(int point = 0; point<lineRenderer.positionCount; point++)
{
Vector3 lineRendererPoint = lineRenderer.GetPosition(point);
edges.Add(new Vector2(lineRendererPoint.x, lineRendererPoint.y));
}
edgeCollider.SetPoints(edges);
}
它很好,但我想让它更顺畅,看到舌头伸出。
发布于 2022-11-14 03:12:18
在统一中平滑地移动到目标点
non-physical movement
1. Transform. Translate. For example: Transform.Translate(Vector3.zero*Time.deltaTime). Move towards (0, 0, 0) at a speed of one meter per second.
2.Vector3.lerp Vector.Slerp, Vector3.MoveTowards.
Vector3.lerp(transform.position,targetposition,Time.deltaTime) . //Slerp is mainly used for the interpolation operation of angle radian. MoveTowards has increased the maximum speed limit on the basis of Lerp.
3. Vector3.SmoothDamp
This method can smoothly move from point A to point B, and can control the speed. The most common usage is that the camera follows the target.
physical movement:
The Rigidbody.MovePosition method in Righdbody is used to move to the target point.
发布于 2022-11-14 15:14:36
用柯鲁丁怎么样?你可以用各种各样的吐温小牛,但我更喜欢赤骨巧克力。
timespan是动画所需的时间。
ExecuteAlways用于编辑器中的测试。与ContextMenu(“StickTongueOut”)一样,您可以在检查器中右键单击script来运行动画。
在OnEnabled中启动舌变量,因为在程序集热重新加载之后,OnEnabled会被触发并重新启动它的值,因此您可以编写在播放时编辑脚本后仍然活着的代码。没必要,但是哦,太方便了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(LineRenderer))]
public class TongueAnimator : MonoBehaviour
{
public Transform target;
public Transform origin;
public float timespan = 1f;
private LineRenderer tongue;
private void OnEnable()
{
tongue = GetComponent<LineRenderer>();
}
private IEnumerator _tongueLoopCoroutine;
/// <summary>
/// This will restart tongue animation coroutine even if it is still out
/// </summary>
public void AnimateTongue(bool direction)
{
if (_tongueLoopCoroutine != null)
StopCoroutine(_tongueLoopCoroutine);
_tongueLoopCoroutine = TongueLoop(direction);
StartCoroutine(_tongueLoopCoroutine);
}
[ContextMenu("StickTongueOut")]
public void StickTongueOut() => AnimateTongue(true);
[ContextMenu("RetractTongue")]
public void RetractTongue() => AnimateTongue(false);
public IEnumerator TongueLoop(bool direction)
{
var startTime = Time.time;
while (startTime + timespan > Time.time)
{
float t = (Time.time - startTime) / timespan;
if (!direction) { t = 1 - t; }
SetPositions(t);
yield return null;
}
SetPositions(direction ? 1 : 0);
//Reusing same code. Local Methods might not be supported in older Unity versions like 2019
void SetPositions(float t)
{
tongue.SetPosition(0, origin.position);
var pos = Vector3.Lerp(origin.position, target.position, t);
tongue.SetPosition(1, pos);
}
}
}
如果您需要EaseInOut,您可以在改变方向之前重新映射t,如下所示:
using UnityEngine;
public static class Utility
{
public static float SmoothStep(this float t) => t * t * (3f - 2f * t);
public static float SmootherStep(this float t) => t * t * t * (t * (t * 6 - 15) + 10);
public static float InverseSmoothStep(this float t) => .5f - Mathf.Sin(Mathf.Asin(1f - 2f * t) / 3f);
public static float SinStep(this float t) => Mathf.Sin(Mathf.PI * (t - .5f)) / 2 + .5f;
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <param name="p">When equals to 2 returns similar results as Smoothstep</param>
/// <returns></returns>
public static float SmoothStepParametric(this float t, float p = 2f)
{
var tp = Mathf.Pow(t, p);
return tp / (tp + Mathf.Pow(1 - t, p));
}
}
这个实用程序允许您这样做。
t = t.SmoothStep();
t = Utility.SmoothStep(t);
只要小心使用没有名称空间的实用程序来破坏您的项目,您将陷入冲突。最好使用名称空间。
https://stackoverflow.com/questions/74424292
复制相似问题