问题描述
您好,我正在为第一人称射击游戏制作后坐脚本。我的枪运转得很好。这是我的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunRecoil : MonoBehavIoUr
{
public Vector3 beforeRecoilRotation,rightBeforeRecoilRotation,leftBeforeRecoilRotation;
//I have a separate script controlling maxRecoilX and recoil
public float maxRecoilX = -0,recoilSpeed = 10,recoil = 0;
[Serializefield]
GameObject leftControl,rightControl,weapon;
public bool once = false;
// Start is called before the first frame update
void Recoiling()
{
//if recoil is present
if (recoil > 0)
{
//make amount of recoil
var maxRecoil = Quaternion.Euler(maxRecoilX,0);
//move gun's rotation,according to recoil
weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation,maxRecoil,Time.deltaTime * recoilSpeed);
//subtract recoil by time.
recoil -= Time.deltaTime;
}
else
{
//make sure recoil is Now zero
recoil = 0;
//make min recoil,based on starting position
Quaternion minRecoil = Quaternion.Euler(beforeRecoilRotation);
Quaternion minRecoilHandRight = Quaternion.Euler(0,0);
weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation,minRecoil,Time.deltaTime * recoilSpeed / 2);
// rightControl.transform.localRotation = Quaternion.Slerp(rightControl.transform.localRotation,minRecoilHandRight,Time.deltaTime * recoilSpeed / 2);
maxRecoilX = 0;
}
}
// Update is called once per frame
void Update()
{
//constantly run Recoiling
Recoiling();
}
}
我也有拿枪的手。我希望他们跟随枪的旋转,直到我的枪的旋转。 首先,我尝试将反冲力套在手上。结果差强人意,但不够接近。我用了类似的东西
var maxRecoilRight = Quaternion.Euler(maxRecoilX *5f,0);
rightControl.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation,maxRecoilRight,Time.deltaTime * recoilSpeed);
结果如下: recoil One
我也尝试使用快速IK:https://assetstore.unity.com/packages/tools/animation/fast-ik-139972#reviews。 这没用,因为我在动画中使用了手臂的各个部分 我也不能将手放在枪上,因为我将不得不替换动画,这可能会破坏我的通用装备。
在Unity中,我可以使用哪种数学来重新定位我的手?请给我任何建议。谢谢,祝您有个愉快的一天/晚上。
解决方法
您可以在反冲进行时关闭手的动画,并在该时间使用IK解决方案。然后,当后坐力结束时,您可以将其重新打开。