我是使用拖尾组件实现的漂移胎痕。
效果
拖尾制作:
代码:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class TrailMgr : MonoSingleton<TrailMgr>
{
private float time;
private Transform parent;
public GameObject trail;
public List<Transform> wheels;
bool isTrail;
private void Start()
{
time = trail.GetComponent<TrailRenderer>().time;
parent = GameObject.Find("Trails").transform;
}
public void InsTrail()
{
if (isTrail)
return;
wheels.ForEach(_ =>
{
var o = Instantiate(trail);
o.transform.parent = parent;
ConstraintSource source = new ConstraintSource();
source.sourceTransform = _.transform;
source.weight = 1;
var v = _.transform.position;
o.transform.position = new Vector3(v.x, 0.6f, v.z);
o.GetComponent<PositionConstraint>().SetSource(0, source);
});
isTrail = true;
}
public void DriftOver()
{
if (parent.childCount == 0)
return;
parent.gameObject.Children().ForEach(_ =>
{
_.GetComponent<PositionConstraint>().constraintActive = false;
});
isTrail = false;
}
}
ConstraintSource组件配置。也可以不使用该组件自己实现跟随车轮XZ坐标。 最后只要自己设置条件调用拖尾效果即可。最好加上车轮是否接触地面的判断会更真实(我用的方法是车轮的down方向射线判断)。
|