zoukankan      html  css  js  c++  java
  • 使用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;
        }
    
    }
    
    

  • 相关阅读:
    HDU-1702-ACboy needs your help again!(Stack)
    HDU1276-士兵队列训练问题 (Queue)
    HDU1285-确定比赛名次(拓扑+优先队列)
    The Preliminary Contest for ICPC Asia Nanjing 2019
    拓扑排序板子 hihocoder-1174
    BZOJ1066 [SCOI2007]蜥蜴
    BZOJ3888 [Usaco2015 Jan]Stampede
    BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径
    BZOJ1112 [POI2008]砖块Klo
    BZOJ1031 [JSOI2007]字符加密Cipher
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14423684.html
Copyright © 2011-2022 走看看