翻墙:
void ProcessVault()
{
bool isVault = false;
if (anim.GetFloat(SpeedZID) > 3 && anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
{
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up * 0.3f, transform.forward, out hit, 4f))
{
if (hit.collider.tag == "Obstacle")
{
if (hit.distance > 3f)
{
Vector3 point = hit.point;
point.y = hit.collider.transform.position.y + hit.collider.bounds.size.y + 0.1f;
matchTarget = point;
isVault = true;
}
}
}
}
anim.SetBool(VaultID, isVault);
if (anim.IsInTransition(0) == false && anim.GetCurrentAnimatorStateInfo(0).IsName("Vault"))
{
anim.MatchTarget(matchTarget, Quaternion.identity, AvatarTarget.LeftHand, new MatchTargetWeightMask(Vector3.one, 0), 0.32f, 0.4f);
}
characterController.enabled = anim.GetFloat(ColliderID) <= 0.5f;
}
滑行
void ProcessSlider()
{
bool isSlider = false;
if (anim.GetFloat(SpeedZID) > 3 && anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
{
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up * 1.5f, transform.forward, out hit, 4f))
{
if (hit.collider.tag == "Obstacle")
{
if (hit.distance > 2f)
{
Vector3 point = hit.point;
matchTarget = point + transform.forward * 2;
isSlider = true;
}
}
}
}
anim.SetBool(SliderID, isSlider);
if (anim.IsInTransition(0) == false && anim.GetCurrentAnimatorStateInfo(0).IsName("Slider"))
{
anim.MatchTarget(matchTarget, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1,0,1), 0), 0.13f, 0.67f);
}
characterController.enabled = anim.GetFloat(ColliderID) <= 0.5f;
}
扛木头
使用IK动画
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Log")
{
Destroy(other.gameObject);
CarryWood();
}
}
private void CarryWood()
{
UnityLog.SetActive(true);
anim.SetBool(isHoldLogID, true);
}
private void OnAnimatorIK(int layerIndex)
{
if(layerIndex == 1)
{
int weight = anim.GetBool(isHoldLogID) ? 1 : 0;
anim.SetIKPosition(AvatarIKGoal.LeftHand, LeftHand.position);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, weight);
anim.SetIKRotation(AvatarIKGoal.LeftHand, LeftHand.rotation);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, weight);
anim.SetIKPosition(AvatarIKGoal.RightHand, RightHand.position);
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, weight);
anim.SetIKRotation(AvatarIKGoal.RightHand, RightHand.rotation);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, weight);
}
}
|