前言
在该游戏中,玩家将操控一个携带弓弩的角色,在有限的时间内尽量获得更多的积分,积分可以通过射中靶子获得,靶子有静止靶和移动靶,不同靶子得分不同。玩家必须要进入指定的射击区内才可以进行射击,射击区的射击数有限。
一、制作角色player
使用一个Capsule胶囊体来作为角色的身体,并把弓和一个摄像机作为胶囊体的子对象,调整摄像机和弓的位置使得画面合理。

二、角色控制
1.MouseLook视角控制
该脚本挂在在摄像机与弓上,使得视角能够随鼠标的运动而转动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
// Start is called before the first frame update
public float mouseSensitivity = 100f; //鼠标灵敏度
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);// limit the angle
// rotate the camera within Y axis
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
// rotate the player within X axis
playerBody.Rotate(Vector3.up * mouseX);
}
}
2.PlayerMovement角色移动
该脚本实现了通过WASD来操作角色移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
private float gravity = 9.8f;
// Start is called before the first frame update
Vector3 move;
void Start()
{
}
// Update is called once per frame
void Update()
{
if(controller.isGrounded){
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Vector3 move = new Vector3(x, 0f, z);// × global movement, we dont want
move = transform.right * x + transform.forward * z;// move along the local coordinates right and forward
}
move.y = move.y - gravity*Time.deltaTime;
controller.Move(move * speed * Time.deltaTime);
}
}
三、Terrain地形制作
创建一个Terrain地形,使用下载的资源将地形染绿,然后用黄色的地形刷子刷出黄色的道路,再往上面加一些花草、树木、房子、山进行装饰,便能做出一块简单的地形。
四、实现天空盒切换SkyboxSwitcher
用一个数组skyboxMaterials存储多个天空盒材质,并在按下Q时进行切换,把该脚本挂载在摄像机上,并在Inspector上配置想要的天空盒材质。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkyboxSwitcher : MonoBehaviour
{
public Material[] skyboxMaterials; // 存储不同天空盒的材质
private int currentSkyboxIndex = 0; // 当前天空盒的索引
void Start()
{
RenderSettings.skybox = skyboxMaterials[currentSkyboxIndex]; // 初始设置天空盒
}
void Update()
{
// 检测按下 'P' 键
if (Input.GetKeyDown(KeyCode.P))
{
// 切换到下一个天空盒
SwitchSkybox();
}
}
void SwitchSkybox()
{
// 增加索引,确保循环切换
currentSkyboxIndex = (currentSkyboxIndex + 1) % skyboxMaterials.Length;
// 设置新的天空盒材质
RenderSettings.skybox = skyboxMaterials[currentSkyboxIndex];
}
}
五、实现拉弓与射箭ShootController
按下左键时会进行蓄力,蓄力时会给下一次发射的箭提供一个力,按下右键可以把箭发射出去。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShootContr



1万+

被折叠的 条评论
为什么被折叠?



