Skip to content

Instantly share code, notes, and snippets.

@halby24
Created July 6, 2019 03:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halby24/3e63b6bd7946852e21be86683acdea4e to your computer and use it in GitHub Desktop.
Save halby24/3e63b6bd7946852e21be86683acdea4e to your computer and use it in GitHub Desktop.
Unityのエディタ上で視線IKが動かせるスクリプトだよ
using UnityEngine;
[ExecuteInEditMode]
public class LookAtIKInEditMode : MonoBehaviour
{
[SerializeField] private bool active;
[SerializeField] private Transform target;
[SerializeField, Range(0f, 1f)] private float lookAt;
[SerializeField, Range(0f, 1f)] private float body;
[SerializeField, Range(0f, 1f)] private float head;
[SerializeField, Range(0f, 1f)] private float eye;
[SerializeField, Range(0f, 1f)] private float clamp;
private Animator _animator;
// Start is called before the first frame update
void Start()
{
_animator = GetComponent<Animator>();
}
private void Update()
{
_animator.Update(0);
}
private void OnAnimatorIK(int layerIndex)
{
if (active)
{
_animator.SetLookAtWeight(lookAt, body, head, eye, clamp);
_animator.SetLookAtPosition(target.position);
}
else
{
_animator.SetLookAtWeight(0);
_animator.SetLookAtPosition(_animator.bodyPosition + _animator.bodyRotation * new Vector3(0, 0.5f, 1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment