Unity3D效果实现:反动力学
要求:让角色无论在哪个位置哪个时刻都盯着一个物体看
实现思路:开启反向动力学(IK),先决定手的位置,让人物的手一直对着物体
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IKControl : MonoBehaviour
{
public Transform Target;
private Animator ani;
// Start is called before the first frame update
void Start()
{
ani = GetComponent<Animator>();
}
// Update is called once per frame
private void OnAnimatorIK(int layerIndex)
{
//设置头部权重
ani.SetLookAtWeight(1);
//设置头部IK注视的目标位置
ani.SetLookAtPosition(Target.position);
//设置右手IK位置的权重,除此之外还可设置左手和双脚的权重
ani.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
//如果需要影响旋转,可以设置旋转权重
ani.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
//设置右手IK位置,除此之外也可设置IK旋转(ani.SetIKRotation())
ani.SetIKPosition(AvatarIKGoal.RightHand, Target.position);
}
}

浙公网安备 33010602011771号