Unity Sample Bootcamp

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3542474.html



M4枪 射击特效#

Gun.js源码#

    function GenerateGraphicStuff(hit : RaycastHit)
    {
        var hitType : HitType;
        
        var body : Rigidbody = hit.collider.rigidbody;
        if(body == null)
        {
            if(hit.collider.transform.parent != null)
            {
                body = hit.collider.transform.parent.rigidbody;
            }
        }
        
        if(body != null)
        {
            if(body.gameObject.layer != 10 && !body.gameObject.name.ToLower().Contains("door"))
            {
                body.isKinematic = false;
            }
        
            if(!body.isKinematic)
            {
                    var direction : Vector3 = hit.collider.transform.position - weaponTransformReference.position;
                body.AddForceAtPosition(direction.normalized * pushPower, hit.point, ForceMode.Impulse);
            }
        }
        
        var go : GameObject;
        
        var delta : float = -0.02;
        var hitUpDir : Vector3 = hit.normal;
        var hitPoint : Vector3 = hit.point + hit.normal * delta;
        
        switch(hit.collider.tag)
        {
            case "wood":
                hitType = HitType.WOOD;
                go = GameObject.Instantiate(woodParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "metal":
                hitType = HitType.METAL;
                go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "car":
                hitType = HitType.METAL;
                go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "concrete":
                hitType = HitType.CONCRETE;
                go = GameObject.Instantiate(concreteParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "dirt":
                hitType = HitType.CONCRETE;
                go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "sand":
                hitType = HitType.CONCRETE;
                go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            case "water":
                go = GameObject.Instantiate(waterParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
                break;
            default:
                return;
        }
        
        go.layer = hit.collider.gameObject.layer;
        
        if(hit.collider.renderer == null) return;
        
        if(timerToCreateDecal < 0.0 && hit.collider.tag != "water")
        {
            go = GameObject.Instantiate(bulletMark, hit.point, Quaternion.FromToRotation(Vector3.forward, -hit.normal));
            var bm : BulletMarks = go.GetComponent("BulletMarks");
            bm.GenerateDecal(hitType, hit.collider.gameObject);
            timerToCreateDecal = 0.02;
        }
    }

分析#

根据枪射击到不同的材质,实例化不同的特效

particle_dirt2particle_dirt

子弹打到的痕迹#

子弹打在材质上表面留下的痕迹:贴花系统,没怎么看明白,Decal System

文档资料#

http://game.ceeger.com/Components/shader-NormalDecal.html

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-textureCoord.html

BulletMarks.js 重点代码#

#pragma strict
#pragma implicit
#pragma downcast

enum HitType
{
    CONCRETE,
    WOOD,
    METAL,
    OLD_METAL,
    GLASS,
    GENERIC
}

class BulletMarks extends MonoBehaviour
{
    public var concrete : Texture2D[];
    public var wood : Texture2D[];
    public var metal : Texture2D[];
    public var oldMetal : Texture2D[];
    public var glass : Texture2D[];
    public var generic : Texture2D[];
    
    public function GenerateDecal(type : HitType, go : GameObject) 
    {
        var useTexture : Texture2D;
        var random : int;
        switch(type)
        {
            case HitType.GENERIC:
                if(generic == null) return;
                if(generic.Length == 0) return;
                
                random = Random.Range(0, generic.Length);
                
                useTexture = generic[random];
                break;
            .......
            default:
                if(wood == null) return;
                if(wood.Length == 0) return;
                
                random = Random.Range(0, wood.Length);
                
                useTexture = wood[random];
                return;
        }
        
        transform.Rotate(new Vector3(0, 0, Random.Range(-180.0, 180.0)));

        Decal.dCount++;
        var d : Decal = gameObject.GetComponent("Decal");
        d.affectedObjects = new GameObject[1];
        d.affectedObjects[0] = go;
        d.decalMode = DecalMode.MESH_COLLIDER;
        d.pushDistance = 0.009 + BulletMarkManager.Add(gameObject);
        var m : Material = new Material(d.decalMaterial);
        m.mainTexture = useTexture;
        d.decalMaterial = m;
        d.CalculateDecal();
        d.transform.parent = go.transform;
    }

 贴图#

imageimage

imageimage

imageimage

作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(916)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
CONTENTS
点击右上角即可分享
微信分享提示