第一人称射击游戏制作

1.创建角色和摄像机

在Unity中创建一个3D角色模型和一个摄像机,将角色模型拖拽到场景中,并将其设置为Player对象,将摄像机拖拽到场景中,并将其设置为Head对象。

2.设置摄像机参数

在Head对象上添加一个CinemachineFreeLooK脚本,用于实现自由视角,在CinemachineFreeLook脚本中,可以设置摄像机的旋转速度、  平滑度等参数。

3.编写控制角色移动的代码

在Player 对象上添加一个Rigidbody组件和一个CharacterController组件,在CharacterController组件中,可以设置角色的移动速度、跳跃高度等参数,然后,编写以下代码来控制角色的移动:

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

public class jump : MonoBehaviour
{

    // Start is called before the first frame update
    private Rigidbody rg;
    public bool isground = true;
    private AudioSource footvoice;
    void Start()
    {
        rg = GetComponent<Rigidbody>();
        footvoice = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isground)
        {

            rg.AddForce(Vector3.up * 200);
        }
        if ((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) && isground)
        {
            if (footvoice.isPlaying == false)
            {
                footvoice.Play();
            }
        }
        else
        {
            footvoice.Stop();
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "ground")
        {

            isground = true;
        }
    }
    private void OnCollisionExit(Collision collision)
    {
        if (collision.collider.tag == "ground")
        {

            isground = false;
        }
    }
}

4.编写控制视角的代码

在Head对象上添加一个 CinemachineFreeLook脚本,并设置好摄像机的旋转速度、平滑度等参数,然后,编写以下代码来控制视角:

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 * 150 * Time.deltaTime);
        }
        float mousey = Input.GetAxis("Mouse Y");
        if (mousey != 0)
        {
            head.Rotate(Vector3.left, mousey * 150 * 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);
        }
        if (Input.GetKey(KeyCode.A))
        {

            body.Translate(Vector3.left * 5.0f * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.D))
        {

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

 5.测试游戏功能

运行游戏,按下鼠标左键时,摄像机会变为自由视角,可以通过鼠标左右移动和上下移动来控制视角,同时,按下W、A、S、D键时,角色会向前、向左、向后、向右移动。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值