unity人物动画和看向某一个物体demo
·
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerContorl : MonoBehaviour
{
// Start is called before the first frame update
public Transform target;
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//获取水平轴
float horizontal = Input.GetAxis("Horizontal");
//获取垂直轴
float vertical = Input.GetAxis("Vertical");
//创建虚拟轴向量
Vector3 dir = new Vector3(horizontal, 0, vertical);
//判断向量是否为0
if (dir != Vector3.zero)
{
//面向向量
transform.rotation = Quaternion.LookRotation(dir);
animator.SetBool("IsRun", true);
transform.Translate(Vector3.forward * Time.deltaTime);
}
else
{
animator.SetBool("IsRun", false);
}
}
private void OnTriggerEnter(Collider other)
{
//Debug.Log("触发");
animator.SetBool("wave", true);
animator.SetBool("IsRun", true);
}
private void OnTriggerStay(Collider other)
{
//Debug.Log("持续触发");
}
private void OnTriggerExit(Collider other)
{
//Debug.Log("触发结束");
animator.SetBool("wave", false);
}
void leftFoot()
{
Debug.Log("左脚");
}
void rightFoot()
{
Debug.Log("右脚");
}
//ik相关的写到这个方法内
private void OnAnimatorIK(int layerIndex)
{
//设置ik权重和看向位置
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(target.position);
//设置右手ik权重
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
//设置旋转权重
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
//设置右手ik位置
animator.SetIKPosition(AvatarIKGoal.RightHand, target.position);
//设置右手ik旋转
animator.SetIKRotation(AvatarIKGoal.RightHand,target.rotation);
}
}
更多推荐
所有评论(0)