87 lines
3.2 KiB
C#
87 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
// ReSharper disable InvertIf
|
|
namespace BITKit
|
|
{
|
|
public class LocationAdditive : MonoBehaviour
|
|
{
|
|
[SerializeField] private bool disabledRotation;
|
|
[SerializeField] private bool disabledPosition;
|
|
|
|
[SerializeField] private Vector3 positionWeight=Vector3.one;
|
|
[SerializeField] private Vector3 rotationWeight=Vector3.one;
|
|
|
|
public Vector3 GlobalPosition { get; private set; }
|
|
public Vector3 GlobalEuler { get; private set; }
|
|
public Vector3 GlobalRotation { get; private set; }
|
|
|
|
private Vector3 currentPosition;
|
|
private Vector3 currentEuler;
|
|
|
|
private Vector3 globalPosition;
|
|
private Quaternion globalRotation;
|
|
|
|
public void AddPosition(Vector3 value)
|
|
{
|
|
currentPosition += value;
|
|
}
|
|
|
|
public void AddEuler(Vector3 value)
|
|
{
|
|
currentEuler += value;
|
|
}
|
|
|
|
public void SetGlobalPosition(Vector3 value)
|
|
{
|
|
globalPosition = value;
|
|
}
|
|
|
|
public void SetGlobalRotation(Quaternion value)
|
|
{
|
|
globalRotation = value;
|
|
}
|
|
|
|
public void LateUpdate()
|
|
{
|
|
currentEuler = Vector3.Scale(currentEuler, rotationWeight);
|
|
currentPosition = Vector3.Scale(currentPosition, positionWeight);
|
|
|
|
currentEuler = Vector3.ClampMagnitude(currentEuler, 4096);
|
|
currentPosition = Vector3.ClampMagnitude(currentPosition, 4096);
|
|
|
|
if (disabledRotation is false)
|
|
switch (currentEuler, globalRotation)
|
|
{
|
|
case var (euler, rotation) when euler.IsDefault() is false && rotation.IsDefault():
|
|
transform.localRotation = Quaternion.Euler(euler);
|
|
break;
|
|
case var (euler, rotation) when euler.IsDefault() && rotation.IsDefault() is false:
|
|
transform.rotation = globalRotation = rotation;
|
|
break;
|
|
case var (euler, rotation) when euler.IsDefault() is false && rotation.IsDefault() is false:
|
|
transform.rotation = globalRotation = rotation * Quaternion.Euler(euler);
|
|
break;
|
|
}
|
|
|
|
if (disabledPosition is false)
|
|
switch (currentPosition, globalPosition)
|
|
{
|
|
case var (position, global) when position.IsDefault() is false && global.IsDefault():
|
|
transform.localPosition = position;
|
|
break;
|
|
case var (position, global) when position.IsDefault() && global.IsDefault() is false:
|
|
transform.position = globalPosition = global;
|
|
break;
|
|
case var (position, global) when position.IsDefault() is false && global.IsDefault() is false:
|
|
transform.position = globalPosition = global + position;
|
|
break;
|
|
}
|
|
|
|
currentEuler = Vector3.zero;
|
|
currentPosition = Vector3.zero;
|
|
}
|
|
}
|
|
} |