使用Animationrigging 使相机靠近角色时角色上半身骨骼转向相机。

配合上眼睛转动看向相机,就可以给角色赋予灵魂。

使用控制骨骼旋转。

 

 

 

首页先要将骨骼Offset调整合适和值,使 控制物体的旋转轴分别对应角色的旋转方向。

 然后通过计算相机和角色不同方向的角度,赋予ik控制对象,控制骨骼旋转。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class IKTargetRotete : MonoBehaviour
{
    public Vector3 selfPointOffset = new Vector3(0,1.2f,1);
    public Transform target;
    public Transform self;
    public Transform bone;
    public float xClimpAngle = 40;
    public float yClimpAngle = 40;
    public float xMut=1,yMut=1;
    public float rotSpeed=30;
    [Range(-180,180)]
    public float selfAngleOffsetValue = 0;
    float offset = 0;
    //public Vector3 rotol;
    private void Awake()
    {
        offset = selfAngleOffsetValue/180;
    } 
    private void LateUpdate()
    {
        float length = (self.position - target.position).magnitude;
        if (length > 10f)
        {
           
            return;
        }
        else if(length>5f)
        {
            transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(Vector3.zero),((length - 5) / 5));
        }
        else
        {
            SetRotation();
        }

        
    }
    void SetRotation()
    {
        Vector3 dir = (target.position - (self.position + selfPointOffset)).normalized;
        float dotz = Vector3.Dot(dir, self.forward);
        Vector3 rot = Vector3.zero;
        float dotx = Vector3.Dot(dir, self.right);
        dotx = dotx + offset;
        rot.y = dotx * xClimpAngle * xMut * Mathf.Abs(dotz);

        float doty = Vector3.Dot(dir, self.up);
        rot.x = -doty * yClimpAngle * yMut;
        transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(rot), Time.deltaTime * rotSpeed);
        //rotol = rot;
    }

}

 


 

posted @ 2021-02-20 22:36  wsfw  阅读(213)  评论(0编辑  收藏  举报