summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Test/FootIKTest.cs
blob: 5b9396198a1f740a3925d56ea7eeb379fcf0c4e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FootIKTest : MonoBehaviour
{

	Animator anim;

	public LayerMask layerMask;

	[Range(0, 1f)]
	public float DistanceToGround;

	private void Start()
	{

		anim = GetComponent<Animator>();

	}

	private void OnAnimatorIK(int layerIndex)
	{

		if (anim)
		{

			anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, anim.GetFloat("IKLeftFootWeight"));
			anim.SetIKRotationWeight(AvatarIKGoal.LeftFoot, anim.GetFloat("IKLeftFootWeight"));
			anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, anim.GetFloat("IKRightFootWeight"));
			anim.SetIKRotationWeight(AvatarIKGoal.RightFoot, anim.GetFloat("IKRightFootWeight"));

			// Left Foot
			RaycastHit hit;
			Ray ray = new Ray(anim.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down);
			if (Physics.Raycast(ray, out hit, DistanceToGround + 1f, layerMask))
			{
                if (hit.transform.tag == "Walkable")
                {
                    Vector3 footOriginForward = anim.GetBoneTransform(HumanBodyBones.LeftFoot).forward;
                    Vector3 footPosition = hit.point; 
					footPosition.y += DistanceToGround; 
					anim.SetIKPosition(AvatarIKGoal.LeftFoot, footPosition);
					anim.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.LookRotation(footOriginForward - Vector3.Dot(footOriginForward, hit.normal) *(hit.normal), hit.normal));
				}
			}

			// Right Foot
			ray = new Ray(anim.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down);
			if (Physics.Raycast(ray, out hit, DistanceToGround + 1f, layerMask))
			{

                if (hit.transform.tag == "Walkable")
                {
                    Vector3 footOriginForward = anim.GetBoneTransform(HumanBodyBones.RightFoot).forward;
                    Vector3 footPosition = hit.point;
					footPosition.y += DistanceToGround;
					anim.SetIKPosition(AvatarIKGoal.RightFoot, footPosition);
					anim.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.LookRotation(footOriginForward - Vector3.Dot(footOriginForward, hit.normal) * (hit.normal), hit.normal));
                }

            }


		}

	}

}