[英]Is there a way to rotate an object that is a child of a parent with animator?
The animator make the object to rotate so the script I'm using with a raycast is not working.动画师让 object 旋转,所以我在光线投射中使用的脚本不起作用。 Only if I disable the animator then when the raycast hit an item the head of the character will rotate look at the item.
只有当我禁用动画师时,当射线投射到一个项目时,角色的头部才会旋转查看该项目。
but is there a way to make that the player head will rotate look at the item even if the animator is still working?但是即使动画师仍在工作,有没有办法让玩家头部旋转查看项目?
The script that attach to the player:附加到播放器的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
public Transform objToRotateLookAT;
private bool raycastSucceed;
// Start is called before the first frame update
void Start()
{
}
void FixedUpdate()
{
int layerMask = 1 << 8;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
if (!raycastSucceed)
Debug.Log("Did Hit");
raycastSucceed = true;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
Vector3 relativePos = hit.transform.position - objToRotateLookAT.position;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
objToRotateLookAT.rotation = rotation;
}
else
{
if (raycastSucceed)
Debug.Log("Did not Hit");
raycastSucceed = false;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.yellow);
}
}
}
Well, you can't make that in Player's animator because you disabled it.好吧,你不能在 Player 的 animator 中做到这一点,因为你禁用了它。 But you can change its rotation in another game object.
但是你可以在另一个游戏 object 中改变它的旋转。 For example, you can put a HeadManager gameObject with an Animator inside.
例如,您可以在其中放置一个带有 Animator 的 HeadManager 游戏对象。 And whenever you want to make the head rotate, you can just make the Bool in animator of HeadManager.
而当你想让头部旋转时,你可以在 HeadManager 的动画师中制作 Bool。
If you have more than 1 object that you want to rotate their head, you can make this HeadManager child of that game object.如果您有超过 1 个 object 想要旋转他们的头部,您可以将这个 HeadManager 设为该游戏 object 的子项。 And when you hit, you can just simply get it's children's animator and make the bool true.
当你击中时,你可以简单地得到它的儿童动画师并使布尔值为真。
https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html
https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html
You can check this to get the children's animator.您可以检查此项以获取儿童动画师。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.