您当前的位置: 首页 >  unity

Unity汽车漂移轮胎印胎痕效果实现

发布时间:2021-10-11 18:42:48 ,浏览量:6

我是使用拖尾组件实现的漂移胎痕。

效果 在这里插入图片描述

拖尾制作: 在这里插入图片描述

代码:

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组件让拖尾跟随轮胎,也可以自己实现 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<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方向射线判断)。

关注
打赏
1688896170
查看更多评论

暂无认证

  • 6浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0532s