Unity Cs项目通过射线发射子弹,生成弹印,资源管理类的运用

在现在的枪战类型游戏里面,子弹的的实例已经转变成了子弹弹印的生成,以及开火特效的生成,让玩家以为是子弹击中目标并出现子弹印记。

玩家第一人称移动:通过资源管理类里面获取所需要的属性。

 Vector3 v = transform.forward*z+ transform.right * x;

锁定第一次视角的方向,这样可以避免转换方向时,出现水平垂直轴混乱的情况。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class PlayerACR : MonoBehaviour
{
    private Transform pos;//空物体的位置
    private GameObject hole;//子弹蛋壳印
    private GameObject fire;//子弹火光特效

    void Start()
    {
        cc =GetComponent<CharacterController>();//获取角色控制器
        an = GetComponent<Animation>();//动画组件

        
        audios = GetComponent<AudioSource>();//音频组件
        clip = Resources.Load<AudioClip>(GameResources.Walk);//动态获取走路的声音效果
        FireClip = Resources.Load<AudioClip>(GameResources.PlayerMShoot);//动态获取开火的声音效果
        pos = GameObject.FindWithTag(GameResources.PlayerPos).transform;//开火点的位置(枪口位置)
        hole = Resources.Load<GameObject>(GameResources.ButtleHole);//子弹弹壳印
        fire = Resources.Load<GameObject>(GameResources.PlayerFire);//子弹火光
    }


    void Update()
    {
        PlayerMove();
        Fire();       
    }
     void PlayerMove()//玩家移动
    {
        //玩家移动
        float x = Input.GetAxis(GameResources.h);
        float z = Input.GetAxis(GameResources.v);
        
        //玩家第一人称方向
        Vector3 v = transform.forward*z+ transform.right * x;
        //数学函数库,x,z的绝对值,判定是否按下水平键
        if (Mathf.Abs(x)!=0||Mathf.Abs(z)!=0)
        {
            //判断音频是否在播出
            if (!audios.isPlaying)
            {
                audios.clip = clip;
                audios.Play();
            }
        //角色控制器,自带重力效果,玩家移动           
            cc.SimpleMove(v * WalkMove * Time.deltaTime);
        }
        
    }


}

玩家开火,弹壳印的生成,以及开火特效的位置

void Fire()//开枪
    {
    
        if (Input.GetMouseButtonDown(0))        
        {           
            //开火动画
            an.Play(GameResources.PlayerFireA);
            
            //开火特效的生成
            GameObject fireLight = Instantiate(fire, pos.position, transform.rotation);
            
              //对子弹开火特效进行微调,根据需求调整
            fireLight.transform.Rotate(new                         Vector3(transform.rotation.x,transform.position.y+180, transform.rotation.z));
            Destroy(fireLight, 0.1f);
            //子弹声音从摄像机位置发出(开火声音,相机位置,音量最大1)
            AudioSource.PlayClipAtPoint(FireClip, Camera.main.transform.position, 1);
            //射线击中的位置
            RaycastHit hit;
            //射线生成位置,射线方向,射线长度
            if (Physics.Raycast(pos.position, transform.forward, out hit, 100))
            {
                //生成子弹弹印
                GameObject g = Instantiate(hole, hit.point, transform.rotation);
                //LookAt(射线与法线垂直),让子弹弹印特效平铺与表面,避免子弹印垂直与地表
                g.transform.LookAt(hit.point - hit.normal);
                //弹印向上移动0.03,微量调整子弹弹印效果;
                g.transform.Translate(Vector3.back * 0.03f);
                Destroy(g, 5f);
            }

           
        }
        else
        {
            a = Action.Draw;
        }
    }

资管管理类:管理项目所有的资源,根据需要时调用,提高代码的利用率,避免代码的耦合性过高

。其类是静态的,可全局调用内容一般为字符串,枚举,整形等,不继承MonoBehaviour

public static  class GameResources 
{
    //玩家移动   
    public const string h = "Horizontal";
    public const string v = "Vertical";
    //开火
    public const string fire = "Fire1";

    //标签
    public const string PlayerTag = "Player";
    public const string PlayerPos = "Pos";

    //子弹弹印路径
    public const string ButtleHole = "Prefabs/hole";

    //动画,添加组件挂载动画
    public const string PlayerDraw = "Draw";//动画名称
    public const string PlayerFireA = "Fire";
    public const string PlayerReload = "Reload";


    //声音路径
    public const string Walk = "Sounds/landDefault";
    public const string PlayerMShoot = "Sounds/M4_Fire";


    //特效路径
    public const string PlayerFire = "Effects/fire";
}
//动画管理
public enum Action
{
    Draw,Fire,Reload
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值