📒目录
📅终于又到周五了,以前闲的发慌现在忙了却又盼着能摸鱼🤣
💡突发奇想闪电侠⚡我做了再做一个会飞的超人,🏎️闪电侠改变速度的数值就可以,💣但是超人飞的话没头绪了❓老样子不会就去找教程......完工之后直接上效果图吧💻:
📑教程原出处:
小破站Up主勿杉搬运油管教程Vinny GameDev,https://www.youtube.com/watch?v=8_49VvqssMU
游戏资源GitHub: https://github.com/vinnygamedev/FlyingCharacterController/tree/d2d772d502ac34d4650db38ab780cacb082f68a9
🎮下面以博文的形式实现这个小实例
👨💻开工
🌏准备工作
💥首先在GitHub上拿到游戏资源后打开项目,点击窗口 ——》 包资源管理器 ——》 安装Cinemachine.
🚀安装完成后在层级下新建 FreeLookCamera.这里我用默认名字CM FreeLook1
🔥在玩家下创建空对象命名为 CamFollow并放在玩家的颈部区域
💤点击CM FreeLook1把CamFollow拖到Follow和LookAt中
🧐调整参数CM FreeLook1
🌟设置图层和标签
📚编写脚本
🖥️编写飞行控制脚本FlyController和摄像机瞄准脚本CameraAiming
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyController : MonoBehaviour
{
public float moveSpeed;
public float maxFloatHeight = 10;
public float minFloatHeight;
public Camera freeLookCamera;
private float currentHeight;
private Animator anim;
private float xRotation;
// Start is called before the first frame update
void Start()
{
currentHeight = transform.position.y;
anim = GetComponent<Animator>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
//float horizontal = Input.GetAxis("Horizontal");
//float vertical = Input.GetAxis("Vertical");
xRotation = freeLookCamera.transform.rotation.eulerAngles.x;
if (Input.GetKey(KeyCode.W))
{
MoveCharacter();
}
else
{
DisableMovement();
}
currentHeight = Mathf.Clamp(transform.position.y, currentHeight, maxFloatHeight);
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
}
private void MoveCharacter()
{
Vector3 cameraForward = new Vector3(freeLookCamera.transform.forward.x, 0, freeLookCamera.transform.forward.z);
transform.rotation = Quaternion.LookRotation(cameraForward);
transform.Rotate(new Vector3(xRotation,0,0),Space.Self);
anim.SetBool("isFlying",true);
Vector3 forward = freeLookCamera.transform.forward;
Vector3 flyDirection = forward.normalized;
currentHeight += flyDirection.y * moveSpeed * Time.deltaTime;
currentHeight = Mathf.Clamp(currentHeight, minFloatHeight, maxFloatHeight);
transform.position += flyDirection * moveSpeed * Time.deltaTime;
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
}
private void DisableMovement()
{
anim.SetBool("isFlying",false);
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraAiming : MonoBehaviour
{
public CinemachineFreeLook freeLookCamera;
public float zoomSpeed = 5;
public float startFOV;
public float zoomedOutFOV;
public float offSetStart;
public float offSetEnd;
private float targetFOV;
private float targetOffset;
private bool isZoomingOut;
// Start is called before the first frame update
void Start()
{
targetFOV = startFOV;
targetOffset = offSetStart;
}
// Update is called once per frame
void Update()
{
isZoomingOut = Input.GetKey(KeyCode.W);
targetFOV = isZoomingOut ? zoomedOutFOV : startFOV;
targetOffset = isZoomingOut ? offSetEnd : offSetStart;
float newFOV = Mathf.Lerp(freeLookCamera.m_Orbits[1].m_Radius, targetFOV, Time.deltaTime * zoomSpeed);
float newOffset = Mathf.Lerp(freeLookCamera.GetRig(1).GetCinemachineComponent<CinemachineComposer>().m_TrackedObjectOffset.x,targetOffset,Time.deltaTime * zoomSpeed);
CinemachineComposer composer = freeLookCamera.GetRig(1).GetCinemachineComponent<CinemachineComposer>();
composer.m_TrackedObjectOffset.x = newOffset;
freeLookCamera.m_Orbits[1].m_Radius = newFOV;
}
}
🔫为脚本参数赋值
📌实现动作切换
🍊编辑动画器,新建一个bool类型的参数isFlying来判断玩家是否处于飞行状态
🎯添加拖尾组件
🤖️之前我在闪电侠身上装了拖尾组件,这次我也在教程的基础上给超人加上拖尾组件
🙋展开玩家模型找到超人的双手双脚
🎇添加拖尾组件并设置好参数包括宽度、时间、材质
🎖️完工下班!!
㊗️大家周末愉快 ,拜拜👋