Unity项目4 3D求生枪手如何为?
摘要:视频教程: https:www.bilibili.comvideoBV16F7zzqEJF?spm_id_from=333.788.videopod.sections&vd_source=25b783f5f9
视频教程:
https://www.bilibili.com/video/BV16F7zzqEJF?spm_id_from=333.788.videopod.sections&vd_source=25b783f5f945c4507229e9dec657b5bb
1. 项目初始化
创建项目“ServivalShooter”
导入包“Survival Shooter.unitypackage”
导入环境、灯光预设,创建背景音乐物体,隐藏 Unity 自带灯光
2. 玩家导入、移动
导入玩家
给“Player”添加脚本“PlayerMovement”
using UnityEngine;
/// <summary>
/// 控制玩家角色的移动
/// </summary>
public class PlayerMovement : MonoBehaviour
{
/// <summary>
/// 玩家移动速度
/// </summary>
public float speed = 6f;
/// <summary>
/// 玩家刚体组件的引用
/// </summary>
private Rigidbody rb;
/// <summary>
/// 在对象激活时获取刚体组件
/// </summary>
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
/// <summary>
/// 在固定时间间隔内处理物理移动
/// </summary>
private void FixedUpdate()
{
// 获取水平和垂直方向的输入
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// 计算移动向量
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// 移动玩家到新的位置
rb.MovePosition(transform.position + movement * speed * Time.fixedDeltaTime);
}
}
3. 摄像机跟随
CameraFollow.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public float smoothing = 5f;
private GameObject player;
private Vector3 offset;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
transform.position = Vector3.Lerp(transform.position, offset + player.transform.position, smoothing * Time.deltaTime);
}
}
4. 玩家跟随鼠标朝向
设置摄像机
设置地面
PlayerMovem
