Oh! I’m still new to using this site, thank you @SurreyMuso for your tolerance and guidance.
Here are my Scripts bellow:
(Set VR Body)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class VRMap
{
public Transform vrTarget;
public Transform rigTarget;
public Vector3 trackingPositionOffset;
public Vector3 trackingRotationOffset;
public void Map()
{
rigTarget.position = vrTarget.TransformPoint(trackingPositionOffset);
rigTarget.rotation = vrTarget.rotation * Quaternion.Euler(trackingRotationOffset);
}
}
public class VRRig : MonoBehaviour
{
public VRMap head;
public VRMap leftHand;
public VRMap rightHand;
public Transform headConstraint;
private Vector3 headBodyOffset;
void Start()
{
headBodyOffset = transform.position - headConstraint.position;
}
void FixedUpdate()
{
transform.position = headConstraint.position + headBodyOffset;
transform.forward = Vector3.ProjectOnPlane(headConstraint.up, Vector3.up).normalized;
head.Map();
leftHand.Map();
rightHand.Map();
}
}
(Set Body’s IK)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VRFootIK : MonoBehaviour
{
private Animator animator;
public Vector3 footOffset;
[Range(0,1)]
public float rightFootPosWeight = 1;
[Range(0, 1)]
public float rightFootRotWeight = 1;
[Range(0, 1)]
public float leftFootPosWeight = 1;
[Range(0, 1)]
public float leftFootRotWeight = 1;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
private void OnAnimatorIK(int layerIndex)
{
Vector3 rightFootPos = animator.GetIKPosition(AvatarIKGoal.RightFoot);
RaycastHit hit;
bool hasHit = Physics.Raycast(rightFootPos + Vector3.up, Vector3.down, out hit);
if (hasHit)
{
animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rightFootPosWeight);
animator.SetIKPosition(AvatarIKGoal.RightFoot, hit.point + footOffset);
Quaternion rightFootRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);
animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rightFootRotWeight);
animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootRotation);
}
else
{
animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 0);
}
Vector3 leftFootPos = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
hasHit = Physics.Raycast(leftFootPos + Vector3.up, Vector3.down, out hit);
if (hasHit)
{
animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPosWeight);
animator.SetIKPosition(AvatarIKGoal.LeftFoot, hit.point + footOffset);
Quaternion leftFootRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);
animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftFootRotWeight);
animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootRotation);
}
else
{
animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0);
}
}
}
(Set Body’s Animation when user moving)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VRAnimatorController : MonoBehaviour
{
public float speedTreshold = 0.1f;
[Range(0,1)]
public float smoothing = 1;
private Animator animator;
private Vector3 previousPos;
private VRRig vrRig;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
vrRig = GetComponent<VRRig>();
previousPos = vrRig.head.vrTarget.position;
}
// Update is called once per frame
void Update()
{
Vector3 headsetSpeed = (vrRig.head.vrTarget.position - previousPos) / Time.deltaTime;
headsetSpeed.y = 0;
Vector3 headsetLocalSpeed = transform.InverseTransformDirection(headsetSpeed);
previousPos = vrRig.head.vrTarget.position;
float previousDirectionX = animator.GetFloat("DirectionX");
float previousDirectionY = animator.GetFloat("DirectionY");
animator.SetBool("isMoving", headsetLocalSpeed.magnitude > speedTreshold);
animator.SetFloat("DirectionX", Mathf.Lerp(previousDirectionX, Mathf.Clamp(headsetLocalSpeed.x, -1, 1), smoothing));
animator.SetFloat("DirectionY", Mathf.Lerp(previousDirectionY, Mathf.Clamp(headsetLocalSpeed.z, -1, 1), smoothing));
}
}
Based on your answer, maybe it’s becouse of a problem with my Blend Tree Type is 2D Simple Directional?