本项目使用了复杂的动画系统,实现了各种空间移动动作、使用不同枪支打僵尸、以及僵尸的追杀和死亡动画。
b站动画:Unity休闲射击打僵尸_哔哩哔哩_bilibili
1.代码实现
本项目的主要侧重点在动画,因此代码部分较为简单,以下是三个脚本的简介
①Player.cs
这段代码主要实现了玩家角色的功能,包括前后左右移动、冲刺、跳跃等控制方法,布置了三种枪械:ak47、狙击枪、散弹枪,以及切换枪械及使用狙击镜和射击的方式,以及为狙击镜和普通视角各设置了一个相机。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{ public GameObject ak47;
public GameObject sniper1;
public GameObject shortgun1;
GameObject current;
public float walkingspeed;
public float runningspeed;
Animator animator;
public float mouseSensitivity = 100.0f;
public Camera maincamera;
public Camera scopecamera;
public GameObject scopeoverlay;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
current = ak47;
// 隐藏光标
Cursor.visible = false;
// 锁定光标到游戏窗口中心
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
// 按下 Esc 键解锁光标
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
current.SetActive(false);
Debug.Log("alpha1");
ak47.SetActive(true);
current = ak47;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
current.SetActive(false);