Unity 6.3
0 онлайн 67 гостей 3 в системе
Вход

Animator.SetLookAtPosition

Declaration

public void SetLookAtPosition(Vector3 lookAtPosition);

Параметры

Параметр Описание
lookAtPosition Позиция в мировом пространстве, чтобы смотреть на.

Описание

Устанавливает вид персонажа во время анимации.

Используйте этот метод в сочетании с Animator.SetLookAtWeight для определения того, насколько сильно символ должен смотреть в направлении указанной позиции.

Вы можете вызвать Animator.SetLookAtPosition только из обратного вызова MonoBehaviour.OnAnimatorIK или StateMachineBehaviour.OnStateIK. Если вызов из другого контекста, этот метод не имеет эффекта и выводит предупреждение.

Дополнительные ресурсы: Animator.SetLookAtWeight, MonoBehaviour.OnAnimatorIK, StateMachineBehaviour.OnStateIK.

using UnityEngine;

[RequireComponent(typeof(Animator))]
public class SetLookAtPositionExample : MonoBehaviour
{
    // The target to look at
    public Transform target;

    // The weight of the look at. This will determine how much the character will look at the target
    [Range(0, 1)]
    public float weight = 1f;

    Animator m_Animator;

    void Awake()
    {
        m_Animator = GetComponent<Animator>();

        if (target == null)
        {
            Debug.LogError("Target is not set. Please set the target to look at.");
        }
    }

    void OnAnimatorIK(int layerIndex)
    {
        if (m_Animator == null || target == null)
        {
            return;
        }

        // Set the look at weight
        m_Animator.SetLookAtWeight(weight);

        // Set the look at position to the target's position
        m_Animator.SetLookAtPosition(target.position);
    }
}