using System;
using UnityEngine;
using Cinemachine;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;
public class PlayerController : MonoBehaviourPunCallbacks
{
private Animator _playerAnimator;
[Header("Inverse Kinematics")]
[Range(0,1)]
public float DistanceToGround;
public LayerMask IKLayer;
private void OnAnimatorIK(int layerIndex)
{
if (_playerAnimator)
{
float IKLeftFootWeight = _playerAnimator.GetFloat("IKLeftFootWeight");
float IKRightFootWeight = _playerAnimator.GetFloat("IKRightFootWeight");
_playerAnimator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, IKLeftFootWeight);
_playerAnimator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, IKLeftFootWeight);
_playerAnimator.SetIKPositionWeight(AvatarIKGoal.RightFoot, IKRightFootWeight);
_playerAnimator.SetIKRotationWeight(AvatarIKGoal.RightFoot, IKRightFootWeight);
// Left foot
RaycastHit hit;
Ray ray = new Ray(_playerAnimator.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down);
if (Physics.Raycast(ray, out hit, DistanceToGround + 1f, IKLayer))
{
Vector3 footPosition = hit.point;
footPosition.y += DistanceToGround;
_playerAnimator.SetIKPosition(AvatarIKGoal.LeftFoot, footPosition);
Vector3 forward = Vector3.ProjectOnPlane(transform.forward, hit.normal);
_playerAnimator.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.LookRotation(forward,hit.normal));
}
// Right foot
ray = new Ray(_playerAnimator.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down);
if (Physics.Raycast(ray, out hit, DistanceToGround + 1f, IKLayer))
{
Vector3 footPosition = hit.point;
footPosition.y += DistanceToGround;
_playerAnimator.SetIKPosition(AvatarIKGoal.RightFoot, footPosition);
Vector3 forward = Vector3.ProjectOnPlane(transform.forward, hit.normal);
_playerAnimator.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.LookRotation(forward,hit.normal));
}
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter