前言
添加程序搖擺和擺動是為任何FPS游戲添加一些細(xì)節(jié)的非常簡單的方法。但是并不是所以的模型動畫都會配有武器擺動動畫效果,在本文中,將實現(xiàn)如何使用一些簡單的代碼實現(xiàn)武器搖擺和擺動效果,這比設(shè)置動畫來嘗試實現(xiàn)類似效果要容易得多
開始
新增SwayNBobScript代碼
using UnityEngine;
// 武器擺動腳本
public class SwayNBobScript : MonoBehaviour
{
[Header("Sway")]
public float step = 0.01f; // 擺動步長
public float maxStepDistance = 0.06f; // 最大步長距離
Vector3 swayPos; // 擺動位置
[Header("Sway Rotation")]
public float rotationStep = 4f; // 擺動旋轉(zhuǎn)步長
public float maxRotationStep = 5f; // 最大旋轉(zhuǎn)步長
Vector3 swayEulerRot; // 擺動旋轉(zhuǎn)角度
public float smooth = 10f; // 平滑移動速度
float smoothRot = 12f; // 平滑旋轉(zhuǎn)速度
[Header("Bobbing")]
public float speedCurve; // 速度曲線參數(shù)
float curveSin { get => Mathf.Sin(speedCurve); } // 曲線正弦值
float curveCos { get => Mathf.Cos(speedCurve); } // 曲線余弦值
public Vector3 travelLimit = Vector3.one * 0.025f; // 移動限制
public Vector3 bobLimit = Vector3.one * 0.01f; // 擺動限制
Vector3 bobPosition; // 擺動位置偏移量
public float bobExaggeration; // 擺動夸張系數(shù)
[Header("Bob Rotation")]
public Vector3 multiplier; // 擺動旋轉(zhuǎn)系數(shù)
Vector3 bobEulerRotation; // 擺動旋轉(zhuǎn)角度
void Update()
{
GetInput();
Sway();
SwayRotation();
BobOffset();
BobRotation();
CompositePositionRotation();
}
Vector2 walkInput; // 行走輸入
Vector2 lookInput; // 視角輸入
// 獲取輸入
void GetInput()
{
walkInput.x = Input.GetAxis("Horizontal");
walkInput.y = Input.GetAxis("Vertical");
walkInput = walkInput.normalized;
lookInput.x = Input.GetAxis("Mouse X");
lookInput.y = Input.GetAxis("Mouse Y");
}
// 擺動
void Sway()
{
Vector3 invertLook = lookInput * -step;
invertLook.x = Mathf.Clamp(invertLook.x, -maxStepDistance, maxStepDistance);
invertLook.y = Mathf.Clamp(invertLook.y, -maxStepDistance, maxStepDistance);
swayPos = invertLook;
}
// 擺動旋轉(zhuǎn)
void SwayRotation()
{
Vector2 invertLook = lookInput * -rotationStep;
invertLook.x = Mathf.Clamp(invertLook.x, -maxRotationStep, maxRotationStep);
invertLook.y = Mathf.Clamp(invertLook.y, -maxRotationStep, maxRotationStep);
swayEulerRot = new Vector3(invertLook.y, invertLook.x, invertLook.x);
}
// 合成位置和旋轉(zhuǎn)部分
void CompositePositionRotation()
{
transform.localPosition = Vector3.Lerp(transform.localPosition, swayPos + bobPosition, Time.deltaTime * smooth);
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(swayEulerRot) * Quaternion.Euler(bobEulerRotation), Time.deltaTime * smoothRot);
}
// 擺動偏移
void BobOffset()
{
speedCurve += Time.deltaTime * (MovementScript.Instance.isGround ? (Input.GetAxis("Horizontal") + Input.GetAxis("Vertical")) * bobExaggeration : 1f) + 0.01f;
bobPosition.x = (curveCos * bobLimit.x * (MovementScript.Instance.isGround ? 1 : 0)) - (walkInput.x * travelLimit.x);
bobPosition.y = (curveSin * bobLimit.y) - (Input.GetAxis("Vertical") * travelLimit.y);
bobPosition.z = -(walkInput.y * travelLimit.z);
}
// 擺動旋轉(zhuǎn)
void BobRotation()
{
bobEulerRotation.x = (walkInput != Vector2.zero ? multiplier.x * (Mathf.Sin(2 * speedCurve)) : multiplier.x * (Mathf.Sin(2 * speedCurve) / 2));
bobEulerRotation.y = (walkInput != Vector2.zero ? multiplier.y * curveCos : 0);
bobEulerRotation.z = (walkInput != Vector2.zero ? multiplier.z * curveCos * walkInput.x : 0);
}
}
代碼掛載在武器父類上即可
正常情況下,我們還要實現(xiàn)武器在瞄準(zhǔn)時,減低或者禁用武器擺動效果,可以選擇在瞄準(zhǔn)時啟動和禁用SwayNBobScript腳本
public SwayNBobScript swayNBobScript;
private void Update()
{
DetermineAim();
}
//瞄準(zhǔn)
void DetermineAim(){
swayNBobScript.enabled = true;
if (Input.GetMouseButton(1)){
swayNBobScript.enabled = false;
}
}
效果
完結(jié)
贈人玫瑰,手有余香!如果文章內(nèi)容對你有所幫助,請不要吝嗇你的點贊評論和關(guān)注
,以便我第一時間收到反饋,你的每一次支持
都是我不斷創(chuàng)作的最大動力。當(dāng)然如果你發(fā)現(xiàn)了文章中存在錯誤
或者有更好的解決方法
,也歡迎評論私信告訴我哦!
好了,我是向宇
,https://xiangyu.blog.csdn.net
一位在小公司默默奮斗的開發(fā)者,出于興趣愛好,最近開始自學(xué)unity,閑暇之余,邊學(xué)習(xí)邊記錄分享,站在巨人的肩膀上,通過學(xué)習(xí)前輩們的經(jīng)驗總是會給我很多幫助和啟發(fā)!php是工作,unity是生活!如果你遇到任何問題,也歡迎你評論私信找我, 雖然有些問題我也不一定會,但是我會查閱各方資料,爭取給出最好的建議,希望可以幫助更多想學(xué)編程的人,共勉~文章來源:http://www.zghlxwxcb.cn/news/detail-801680.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-801680.html
到了這里,關(guān)于【unity小技巧】實現(xiàn)沒有動畫的FPS武器搖擺和擺動效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!