Unity Udexreal开发插件包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.5 KiB

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UDE_HAND_INTERACTION;
using UnityEngine;
public class DoorActiveSample : MonoBehaviour
{
public Transform Door;
public float ActiveMaxDegree;
public Vector3 ActiveAxis = Vector3.up;
public Vector3 MoveAxis;
public Transform DoorBar;
public float BarActiveMaxDegree = 60f;
public Vector3 BarActiveAxis = Vector3.forward;
private Transform FollowedHand = null;
private Vector3 PositionOffset = Vector3.zero;
private bool LockDoor = true;
public float rate = 15;
private Vector3[] NormalPivots = new Vector3[2] { Vector3.zero, Vector3.zero};
private Vector3 NormalPivot;
void Start()
{
var FrontHand = GetComponent<InteractableObject>().HandInteractions[0].transform;
var BackHand = GetComponent<InteractableObject>().HandInteractions[2].transform;
NormalPivots[0] = FrontHand.position - new Vector3(Door.position.x, FrontHand.position.y, Door.position.z);
NormalPivots[1] = BackHand.position - new Vector3(Door.position.x, BackHand.position.y, Door.position.z);
}
void Update()
{
FollowedHand = GetComponent<InteractableObject>().GetReferenceHand(1);
var FakeHand = GetComponent<InteractableObject>().GetReferenceHand(0);
if (FakeHand)
{
PositionOffset = DoorBar.position - FollowedHand.position;
var HandName = FakeHand.gameObject.name.Split("_")[0];
int Index = HandName == "Front" ? 0 : 1;
NormalPivot = NormalPivots[Index];
}
else
{
DoorBar.DOLocalRotate(Vector3.zero, 0.3f);
return;
}
if (FollowedHand != null)
{
float YaxisValue = PositionOffset.y > 0 ? PositionOffset.y : 0;
DoorBar.localEulerAngles = BarActiveMaxDegree * BarActiveAxis * YaxisValue * rate;
}
float BarActiveRate = Math.Abs(RegularAngle(Vector3.Dot(DoorBar.localEulerAngles, BarActiveAxis)));
if (BarActiveRate >= BarActiveMaxDegree)
{
DoorBar.localEulerAngles = BarActiveMaxDegree * BarActiveAxis;
}
LockDoor = BarActiveRate < BarActiveMaxDegree;
if (!LockDoor || RegularAngle(Vector3.Dot(Door.localEulerAngles, ActiveAxis)) >= 1.5f)
{
Vector3 Pivot2Hand = FollowedHand.position - new Vector3(Door.position.x, FollowedHand.position.y, Door.position.z);
if (Vector3.Dot(Pivot2Hand, MoveAxis) + 0.01f < Vector3.Dot(NormalPivot, MoveAxis)) return;
//Debug.Log(Pivot2Hand + "//" + NormalPivot + "//" + Vector3.Angle(Pivot2Hand, NormalPivot));
Door.localEulerAngles = Vector3.Angle(Pivot2Hand, NormalPivot) * ActiveAxis;
}
float ActiveRate = Vector3.Dot(RegularAngle(Door.localEulerAngles), ActiveAxis);
if (Math.Abs(ActiveRate) >= Math.Abs(ActiveMaxDegree))
{
Door.localEulerAngles = ActiveMaxDegree * -ActiveAxis;
}
}
private Vector3 RegularAngle(Vector3 angle)
{
for(int i = 0; i < 3; ++i)
{
if (angle[i] > 180)
{
angle[i] -= 360;
}
else if(angle[i] < -180)
{
angle[i] += 360;
}
}
return angle;
}
private float RegularAngle(float angle)
{
if (angle > 180)
{
angle -= 360;
}
else if (angle < -180)
{
angle += 360;
}
return angle;
}
}