unity学习46:反向动力学IK

目录

1 正向动力学和反向动力学

1.1 正向动力学

1.2 反向动力学

1.3 实现目标

2 实现反向动力

2.1  先定义一个目标

2.2 动画层layer,需要加 IK pass

2.3 增加头部朝向代码

2.3.1 专门的IK方法  OnAnimatorIK(int layerIndex){}

2.3.2 增加朝向代码

2.4 增加头部朝向代码

2.5  需要设置权重

2.6 需要设置位置position 和 rotation

2.7 具体代码: 头部和手都实现了IK朝向


1 正向动力学和反向动力学

1.1 正向动力学

  • 正常的模型身体的运动
  • 模仿人体的真实的骨骼。

1.2 反向动力学

  • 真实世界中,不存在的,相反的一个骨骼方式
  • 用目标---牵引 手指---牵引手臂,这样反向的指引方式
  • 游戏里IK相关的就是

1.3 实现目标

  • 想实现,玩家角色的眼睛,头部,手,一直指向目标

2 实现反向动力

2.1  先定义一个目标

public class TestPlayer1 : MonoBehaviour

{

    public Transform target1;

    private Animator animator1;

2.2 动画层layer,需要加 IK pass

  • 需要进行反向动力学的动画层,
  • 动画层layer,需要加 IK pass

2.3 增加头部朝向代码

2.3.1 专门的IK方法  OnAnimatorIK(int layerIndex){}

  • 需要专门的IK方法 
  • private void OnAnimatorIK(int layerIndex){}

2.3.2 增加朝向代码

  • animator1.SetLookAtWeight(1);
  • animator1.SetLookAtPosition(target1.position);

    private void OnAnimatorIK(int layerIndex)

    {

        //设置头部IK  Weight=0表示不生效

        animator1.SetLookAtWeight(1);

        animator1.SetLookAtPosition(target1.position);

    }

2.4 增加头部朝向代码

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

public class TestPlayer1 : MonoBehaviour
{
    public Transform target1;
    private Animator animator1;

    // Start is called before the first frame update
    void Start()
    {
        animator1=GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        float horzontal=Input.GetAxis("Horizontal");
        float vetical=Input.GetAxis("Vertical");
        Vector3 dir1=new Vector3(horzontal,0,vetical);
        Debug.DrawRay(transform.position,dir1,Color.red);

        
        //如果按下了移动按键
        if(dir1 != Vector3.zero)
        {
            //面向向量
            transform.rotation=Quaternion.LookRotation(dir1);
            
            //播放跑步动画
            animator1.SetBool("IsRun",true);
            //朝着面向的前方移动
            transform.Translate(Vector3.forward*2*Time.deltaTime);
        }else
        {
            //播放walk动画
            animator1.SetBool("IsRun",false);
        }

        if(Input.GetKeyDown(KeyCode.Q))
        {
            //触发wave参数
            GetComponent<Animator>().SetTrigger("wave");
        }

        //获得曲线的test1参数
        //Debug.Log(animator1.GetFloat("test1"));
        
    }
    void rightfoot()
    {
        Debug.Log("右脚");
    }

    void leftfoot()
    {
        Debug.Log("左脚");
    }

    private void OnAnimatorIK(int layerIndex)
    {
        //设置头部IK  Weight=0表示不生效
        animator1.SetLookAtWeight(1);
        animator1.SetLookAtPosition(target1.position);
    }

}

2.5  需要设置权重

  • 权重=1 表示生效
  • animator1.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
  • animator1.SetIKRotationWeight(AvatarIKGoal.RightHand,1);

       

   private void OnAnimatorIK(int layerIndex)

    {

        //设置头部IK  Weight=0表示不生效

        animator1.SetLookAtWeight(1);

        animator1.SetLookAtPosition(target1.position);

        //设置右手position的IK权重

        animator1.SetIKPositionWeight(AvatarIKGoal.RightHand,1);

        //设置右手旋转IK权重

        animator1.SetIKRotationWeight(AvatarIKGoal.RightHand,1);

        //设置右手IK

        animator1.SetIKPosition(AvatarIKGoal.RightHand,target1.position);

        animator1.SetIKRotation(AvatarIKGoal.RightHand,target1.rotation);

    }

2.6 需要设置位置position 和 rotation

  • //设置右手IK
  • animator1.SetIKPosition(AvatarIKGoal.RightHand,target1.position);
  • animator1.SetIKRotation(AvatarIKGoal.RightHand,target1.rotation);

2.7 具体代码: 头部和手都实现了IK朝向

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

public class TestPlayer1 : MonoBehaviour
{
    public Transform target1;
    private Animator animator1;

    // Start is called before the first frame update
    void Start()
    {
        animator1=GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        float horzontal=Input.GetAxis("Horizontal");
        float vetical=Input.GetAxis("Vertical");
        Vector3 dir1=new Vector3(horzontal,0,vetical);
        Debug.DrawRay(transform.position,dir1,Color.red);

        
        //如果按下了移动按键
        if(dir1 != Vector3.zero)
        {
            //面向向量
            transform.rotation=Quaternion.LookRotation(dir1);
            
            //播放跑步动画
            animator1.SetBool("IsRun",true);
            //朝着面向的前方移动
            transform.Translate(Vector3.forward*2*Time.deltaTime);
        }else
        {
            //播放walk动画
            animator1.SetBool("IsRun",false);
        }

        if(Input.GetKeyDown(KeyCode.Q))
        {
            //触发wave参数
            GetComponent<Animator>().SetTrigger("wave");
        }

        //获得曲线的test1参数
        //Debug.Log(animator1.GetFloat("test1"));
        
    }
    void rightfoot()
    {
        Debug.Log("右脚");
    }

    void leftfoot()
    {
        Debug.Log("左脚");
    }

    private void OnAnimatorIK(int layerIndex)
    {
        //设置头部IK  Weight=0表示不生效
        animator1.SetLookAtWeight(1);
        animator1.SetLookAtPosition(target1.position);

        //设置右手position的IK权重
        animator1.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
        //设置右手旋转IK权重
        animator1.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
        //设置右手IK
        animator1.SetIKPosition(AvatarIKGoal.RightHand,target1.position);
        animator1.SetIKRotation(AvatarIKGoal.RightHand,target1.rotation);

    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/971912.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

DeepSeek 和 ChatGPT 在特定任务中的表现:逻辑推理与创意生成

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;Linux网络编程 &#x1f337;追光的人&#xff0c;终会万丈光芒 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 ​ Linux网络编程笔记&#xff1a; https://blog.cs…

DAY07 Collection、Iterator、泛型、数据结构

学习目标 能够说出集合与数组的区别数组:1.是引用数据类型的一种2.可以存储多个元素3.数组的长度是固定的 int[] arr1 new int[10]; int[] arr2 {1,2,3};4.数组即可以存储基本类型的数据,又可以存储引用数据类型的数据int[],double[],String[],Student[]集合:1.是引用数据类…

ls命令的全面参数解析与详尽使用指南

目录 ls 命令的所有参数及含义 ls -a 命令详解 ls -A 命令详解 ls -b 命令详解 ls -C 命令详解 ls -d 命令详解 ls -f 命令详解 ls -F 命令详解 ls -G 命令详解 ls -h 命令详解 ls -H 命令详解 ls -i 命令详解 ls -I 命令详解 ls -l 命令详解 ls -L 命令详解 l…

【Spring+MyBatis】_图书管理系统(中篇)

【SpringMyBatis】_图书管理系统&#xff08;上篇&#xff09;-CSDN博客文章浏览阅读654次&#xff0c;点赞4次&#xff0c;收藏7次。&#xff08;1&#xff09;当前页的内容records&#xff08;类型为List&#xff09;&#xff1b;参数&#xff1a;userNameadmin&&pas…

动态规划算法篇:枚举的艺术

那么本篇文章就正式进入了动态规划的算法的学习&#xff0c;那么动态规划算法也可谓是算法内容中的一座大山&#xff0c;那么在大厂算法笔试乃至算法比赛中出现的频率也逐渐变高&#xff0c;那么可见学习好动态规划算法的一个重要性&#xff0c;那么对于动态规划最难理解的&…

算法——舞蹈链算法

一&#xff0c;基本概念 算法简介 舞蹈链算法&#xff08;Dancing Links&#xff0c;简称 DLX&#xff09;是一种高效解决精确覆盖问题的算法&#xff0c;实际上是一种数据结构&#xff0c;可以用来实现 X算法&#xff0c;以解决精确覆盖问题。由高德纳&#xff08;Donald E.…

翻转硬币(思维题,巧用bitset)

0翻转硬币 - 蓝桥云课 #include <bits/stdc.h> using namespace std; bitset<200000001> t; int main() {int n;cin>>n;int ans1;t[1]1;int totn-1;for(int i2;i<n;i){if(t[i]) continue;int ji;ans;while(j<n){t[j]!t[j];if(t[j]) tot--;else tot;ji;…

网络安全等级保护测评(等保测评):全面指南与准备要点

等保测评&#xff0c;全称为“网络安全等级保护测评”&#xff0c;是根据《网络安全法》及《网络安全等级保护条例》等法律法规&#xff0c;对信息系统进行安全等级划分&#xff0c;并依据不同等级的安全保护要求&#xff0c;采用科学方法和技术手段&#xff0c;全面评估信息系…

blackbox.ai 一站式AI代理 畅享顶级模型

最近Deepseek火遍大江南北&#xff0c;一夜之间到处都能看到有人在体验AI技术。然而这也带来了一些困难&#xff1a;由于服务器压力过大&#xff0c;ds开始使用了一些限流的措施。 实际上这只是针对免费用户的限制手段&#xff0c;通过API付费方式的用户并没有这样的限制。所以…

ERP对制造业务有何价值?

ERP 的定义 在定义 ERP 之前&#xff0c;我们先从其首字母缩写说起&#xff0c;ERP 代表企业资源规划。我们可以将 ERP 定义为一种企业软件&#xff0c;它帮助组织管理日常业务。从根本上讲&#xff0c;ERP 将客户管理、人力资源、商业智能、财务管理、库存以及供应链功能整合…

(新版本onenet)stm32+esp8266/01s mqtt连接onenet上报温湿度和远程控制(含小程序)

物联网实践教程&#xff1a;微信小程序结合OneNET平台MQTT实现STM32单片机远程智能控制 远程上报和接收数据——汇总 前言 之前在学校获得了一个新玩意&#xff1a;ESP-01sWIFI模块&#xff0c;去搜了一下这个小东西很有玩点&#xff0c;远程控制LED啥的&#xff0c;然后我就想…

详解 本机安装多个MySQL服务【为后续大数据量分库分表奠定基础,以mysql8.0为例,附有图文】

本机安装多个mysql 在电脑上新建mysql8文件夹&#xff0c;然后在mysql8文件下新建mysql3391文件夹。然后找到自己原本mysql的安装目录&#xff0c;我的是E:\software\mysql\one&#xff0c;如图所示&#xff1a; 将次目录下的所有文件全选复制粘贴在mysql3391文件夹下。 然后…

组学数据分析实操系列 |(四) 富集气泡图的绘制

前言:在上一篇中&#xff0c;我们介绍了利用Metascape零代码实现富集分析&#xff0c;但是Metascape的富集分析结果是以柱状图的形式展示的。文章中更常使用的富集结果可视化方式是气泡图。气泡图可以通过气泡的坐标、形状、颜色、大小等来展示更加丰富的富集分析结果&#xff…

浏览器开发者工具(F12)查看请求的响应体内容显示”无法加载响应数据: No resource with given identifier found“

背景 复习在 SSM&#xff08;Spring Spring MVC MyBatis&#xff09;框架中&#xff0c;点击登录请求后返回 JSON 格式的数据&#xff0c;出现只有登录失败的请求才有响应值&#xff0c;比如&#xff1a; {success: false, message: “没有此用户”, code: 400} 而成功的请求…

Deepseek 万能提问公式:高效获取精准答案

### **Deepseek 万能提问公式&#xff1a;高效获取精准答案** 在使用 Deepseek 或其他 AI 工具时&#xff0c;提问的质量直接决定了答案的精准度和实用性。以下是一个万能的提问公式回答&#xff1a; --- ### **1. 明确背景&#xff08;Context&#xff09;** - **作用**…

从月牙定理看古希腊数学的奇妙突破

文章目录 每日一句正能量前言古希腊人的 “化圆为方” 之梦&#xff08;一&#xff09;几何作图的基本规则&#xff08;二&#xff09;化圆为方问题的起源与发展&#xff08;三&#xff09;化圆为方的意义 月牙面积定理的诞生&#xff08;一&#xff09;希波克拉底的生平与成就…

Linux操作系统--信号

目录 1.信号产生 概念&#xff1a; core vs Term 信号产生的5种条件 从软硬件理解信号的保存 软件&#xff0c;如何理解信号处理&#xff1f; 硬件中断 2、信号捕捉 3、信号阻塞 信号其他相关常见概念 1.信号产生 概念&#xff1a; 什么是信号&#xff1f;信号一种用…

太空飞船任务,生成一个地球发射、火星着陆以及下一次发射窗口返回地球的动画3D代码

import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from mpl_toolkits.mplot3d import Axes3D# 天体参数设置&#xff08;简化模型&#xff09; AU 1.5e8 # 天文单位&#xff08;公里&#xff09; earth_orbital_radius …

sql注入之python脚本进行时间盲注和布尔盲注

一、什么是时间盲注和布尔盲注&#xff1f; 答&#xff1a;时间盲注是攻击者通过构造恶意sql语句利用sleep()等延迟函数来观察数据库响应时间差异来进行推断信息和条件判断。如果条件为真&#xff0c;数据库会执行延时操作&#xff0c;如果为假则立即返回。响应时间较短。 SELE…

Python .py文件打包成.exe可执行程序,带托盘图标的可执行文件

Python .py文件打包成.exe可执行程序&#xff0c;带托盘图标的可执行文件 安装pyinstalle 查看是否安装了pyinstaller 已安装 C:\Users\Administrator>pip show pyinstaller Name: pyinstaller Version: 6.12.0 Summary: PyInstaller bundles a Python application and a…