OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class OctoIkRaycast : MonoBehaviour
|
||||
{
|
||||
public float _distance = 1;
|
||||
public Vector3 rotationOffset;
|
||||
public Transform _target;
|
||||
public LayerMask _layerMask;
|
||||
|
||||
Vector3 originPos;
|
||||
Vector3 targetPos;
|
||||
Quaternion originRot;
|
||||
Quaternion targetRot;
|
||||
|
||||
void Start()
|
||||
{
|
||||
originPos = targetPos = _target.localPosition;
|
||||
originRot = targetRot = _target.localRotation;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float deltaPos = Time.deltaTime * 3f;
|
||||
float deltaRot = Time.deltaTime * 3f;
|
||||
|
||||
RaycastHit hit;
|
||||
|
||||
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, _distance, _layerMask))
|
||||
{
|
||||
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
|
||||
|
||||
targetPos = transform.InverseTransformPoint(hit.point);
|
||||
targetRot = Quaternion.LookRotation(-transform.InverseTransformDirection(hit.normal), transform.up) * Quaternion.Euler(rotationOffset);
|
||||
|
||||
deltaPos = Mathf.Lerp(1, deltaPos, Mathf.Clamp01(hit.distance / _distance));
|
||||
deltaRot = Mathf.Lerp(.1f, deltaRot, Mathf.Clamp01(hit.distance / _distance));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * _distance, Color.white);
|
||||
|
||||
targetPos = originPos;
|
||||
targetRot = originRot;
|
||||
}
|
||||
|
||||
_target.localPosition = Vector3.Lerp(_target.localPosition, targetPos, deltaPos);
|
||||
_target.localRotation = Quaternion.Lerp(_target.localRotation, targetRot, deltaRot);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user