我是使用拖尾组件实现的漂移胎痕。
效果
拖尾制作:
代码:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class TrailMgr : MonoSingleton //继承单例,可自己实现
{
private float time; //拖尾持续时间
private Transform parent; //放置拖尾的父物体
public GameObject trail; //拖尾预制体
public List wheels; //获取车轮位置
bool isTrail; //拖尾效果是否持续中
private void Start()
{
time = trail.GetComponent().time; //拖尾消失时间
parent = GameObject.Find("Trails").transform; //获取防止拖尾特效的父物体
}
//生成拖尾
public void InsTrail()
{
if (isTrail)
return;
//生成拖尾效果并跟随轮胎
wheels.ForEach(_ =>
{
var o = Instantiate(trail);
o.transform.parent = parent;
//这里使用ConstraintSource组件让拖尾跟随轮胎,也可以自己实现
ConstraintSource source = new ConstraintSource();
source.sourceTransform = _.transform;
source.weight = 1;
var v = _.transform.position;
//这里需要自己设置Y
o.transform.position = new Vector3(v.x, 0.6f, v.z);
o.GetComponent().SetSource(0, source);
});
isTrail = true;
}
//停止漂移销毁拖尾
public void DriftOver()
{
if (parent.childCount == 0)
return;
parent.gameObject.Children().ForEach(_ =>
{
_.GetComponent().constraintActive = false;
});
isTrail = false;
}
}
ConstraintSource组件配置。也可以不使用该组件自己实现跟随车轮XZ坐标。 最后只要自己设置条件调用拖尾效果即可。最好加上车轮是否接触地面的判断会更真实(我用的方法是车轮的down方向射线判断)。