制作简易3d枪战游戏

今天心血来潮,我打算制作一个简易的3d枪战小游戏。接下来是我的步骤,可以参考参考。

首先我们先下载一个简单的场景地图,随后创建一个我们操作的对象。

在有了操作对象之后我们为它赋予移动的特性。这里我们通过c#的脚本来实现。以下是移动的代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Move : MonoBehaviour

{

    private Transform head;

    private Transform body;

    // Start is called before the first frame update

    void Start()

    {

        head = transform;

        body = transform.parent;

        Cursor.lockState = CursorLockMode.Locked;

    }

    // Update is called once per frame

    void Update()

    {

        float mousex = Input.GetAxis("Mouse X");

      

        if (mousex!=0){

           body.Rotate(Vector3.up, mousex * 100 * Time.deltaTime);

        }

        float mousey = Input.GetAxis("Mouse Y");

        if (mousey != 0)

        {

            head.Rotate(Vector3.left, mousey * 100 * Time.deltaTime);

        }

        if (Vector3.Angle(body.forward, head.forward)>60) {

            head.Rotate(Vector3.left, mousey * -100 * Time.deltaTime);

        }

        if (Input.GetKey(KeyCode.W)) {

            body.Translate(Vector3.forward * 5.0f * Time.deltaTime);

        }

        if (Input.GetKey(KeyCode.S))

        {

            body.Translate(Vector3.back * 5.0f * Time.deltaTime);

在有了人物移动之后我们发现,我们的摄像机并没有跟随人物移动,所以我们可以添加一个摄像机,并且添加脚本使其跟随我们的人物,以下是摄像机移动的代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CamMove : MonoBehaviour

{

    public GameObject Player;

    public Vector3 offset;

    // Start is called before the first frame update

    void Start()

    {

        offset =transform.position- Player.transform.position;

    }

    // Update is called once per frame

    void Update()

    {

        transform.position = Player.transform.position + offset;

    }

}

在有了以上步骤之后我们发现已经逐步完善了人物,那么接下来就是针对游戏道具的创建。

首先我们给枪加上生成和发射子弹的脚本,以下是生成和发射子弹的代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class guncontral : MonoBehaviour

{

    //枪口火焰生成位置

    public Transform firepoint;

    //枪口火焰物体

    public GameObject fireper;

    //子弹生成位置

    public Transform bulletpoint;

    //子弹物体

    public GameObject bulletper;

    //子弹个数

    private int bulletCount=10;

    // 开火间隔

    private float cd = 0.2f;

    //实际开火的时间 计时器

    private float timer=0;

    // Start is called before the first frame update

    void Start()

    {

        

    }

    // Update is called once per frame

    void Update()

    {

        //计算实际开火间隔

        timer = timer + Time.deltaTime;

        if (Input.GetMouseButton(0)&& bulletCount>0&&timer>cd) {

          

            timer = 0;

            //火焰生成

            Instantiate(fireper, firepoint.position, firepoint.rotation);

            //子弹生成

            Instantiate(bulletper, bulletpoint.position, bulletpoint.rotation);

            bulletCount--;

在添加上生成和发射子弹的脚本之后我们可以添加换子弹的动作。以下是换子弹的脚本代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class guncontral : MonoBehaviour

{

    //枪口火焰生成位置

    public Transform firepoint;

    //枪口火焰物体

    public GameObject fireper;

    //子弹生成位置

    public Transform bulletpoint;

    //子弹物体

    public GameObject bulletper;

    //子弹个数

    private int bulletCount=10;

    // 开火间隔

    private float cd = 0.2f;

    //实际开火的时间 计时器

    private float timer=0;

    private AudioSource gunvoice;

    public AudioClip clip;

    // Start is called before the first frame update

    void Start()

    {

        gunvoice = GetComponent<AudioSource>();

    }

    // Update is called once per frame

    void Update()

    {

        //计算实际开火间隔

        timer = timer + Time.deltaTime;

        if (Input.GetMouseButton(0)&& bulletCount>0&&timer>cd) {

          

            timer = 0;

            //火焰生成

            Instantiate(fireper, firepoint.position, firepoint.rotation);

            //子弹生成

            Instantiate(bulletper, bulletpoint.position, bulletpoint.rotation);

            bulletCount--;

            gunvoice.PlayOneShot(clip);

        }

        if (bulletCount == 0) {

            //Invoke("Reload", 2.0f);

            GetComponent<Animator>().SetTrigger("Reload");

            Reload();

        }

        if (Input.GetKeyDown(KeyCode.R)&& bulletCount!=10) {

            GetComponent<Animator>().SetTrigger("Reload");

            Reload();

        }

    }

    void Reload() {

        bulletCount = 10;

    }

}

好了,这样我们的枪战小游戏人物阶段就完成了,大家快去试试吧!

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值