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

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

效果
在这里插入图片描述

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

代码:

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方向射线判断)。

猜你喜欢

转载自blog.csdn.net/qq_39162826/article/details/120708839