【发布时间】:2018-02-12 06:13:48
【问题描述】:
目标
我想使用脚本在给定 x、y、z 旋转值的情况下旋转人形角色的头部(不使用场景中的任何其他对象作为“看”方向)。
我想做什么
设置人形角色的绑定时(humanoid prefab --> Rig --> Animation Type: Humanoid --> Configure --> Muscles & Settings),您会看到以下界面:https://docs.unity3d.com/Manual/MuscleDefinitions.html
在此菜单中,在 Head 下,您可以拖动滑块进行移动,例如你的人形头部上下。我想用一个脚本来达到同样的效果,但我不知道该怎么做。
资源
这个问题从未通过示例代码得到正确答案:https://answers.unity.com/questions/824628/can-we-using-script-to-moving-the-muscles-in-unity.html
我想我必须对HumanPose.muscles (https://docs.unity3d.com/ScriptReference/HumanPose-muscles.html) 做点什么,但是由于缺少代码示例,我不知道如何解决这个问题。
编辑 3:此链接包含 HumanPose 的代码示例,但我还没有让它工作:https://forum.unity.com/threads/humanposehandler.430354/
问题
如何获取人形角色的头部肌肉并通过脚本赋予值来旋转它们? (或任何其他方式如何使用头部旋转值旋转头部,而场景中没有另一个对象)。任何帮助将不胜感激。
编辑 1 & 2:示例代码
我收到一条 JSON 格式的消息,我从中提取弧度值并将其更改为度数:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json.Linq; // JSON reader; https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347
public class HeadRotator : MonoBehaviour {
Quaternion rotation = Quaternion.identity;
// Radians to degree
float Rad2Degree = 180 / Mathf.PI;
// Human muscle stuff
HumanPoseHandler humanPoseHandler;
HumanPose humanPose;
Animator anim;
//Transform head;
// Use this for initialization
void Start () {
// get attached Animator controller
anim = GetComponent<Animator>();
//head = anim.GetBoneTransform(HumanBodyBones.Head);
//Debug.Log (head);
}
// Use JSON message to set head rotation and facial expressions;
// IEnumerator to run in main thread (needed for SetBlendShapeWeight)
public IEnumerator RequestHeadRotation(JObject head_pose)
{
// rotate head of character with received x, y, z rotations in radian
List<float> head_rotation = new List<float> ();
foreach (KeyValuePair<string, JToken> pair in head_pose) {
//Debug.Log(pair);
// store head rotation in degrees
head_rotation.Add(float.Parse(pair.Value.ToString())*Rad2Degree*10);
}
Debug.Log ("" + head_rotation[0] + ", " + head_rotation[1] + ", " + head_rotation[2]);
//Quaternion rotation = Quaternion.Euler(new Vector3(head_rotation[0], head_rotation[1], head_rotation[2]));
//Debug.Log ("" + rotation[0] + ", " + rotation[1] + ", " + rotation[2] + ", " + rotation[3]);
// head.Rotate (rotation);
// https://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
rotation.eulerAngles = new Vector3 (head_rotation[0], head_rotation[1], head_rotation[2]);
// Animator.GetBoneTransform()
anim.SetBoneLocalRotation(HumanBodyBones.Head, rotation);
yield return null;
}
}
- 头部旋转数据:
3.208564, 0.4583662, 0.1145916 - 四元数数据:
0.0280001, 0.003970424, 0.0008876149, 0.9995997
我不太清楚如何设置头骨或肌肉。大多数示例仅提供代码的 sn-p,因此我正在努力弄清楚它是如何工作的。
编辑 2:我觉得我快接近了,但 anim.SetBoneLocalRotation(HumanBodyBones.Head, rotation); 似乎被忽略了。
编辑 4:我在 Github 上放了一个简单版本的头部旋转尝试:https://github.com/NumesSanguis/Basic-Unity-Head-Rotation
【问题讨论】:
-
为什么不从脚本中旋转与头部相关的骨骼?
-
你能告诉我你是怎么做到的吗?我添加了一个代码 sn-p,其中包含我如何获取旋转值,但不知道如何将其应用于头骨。
-
人形机器人会被动画化吗?
标签: unity3d