87 lines
1.6 KiB
C#
87 lines
1.6 KiB
C#
using System;
|
|
using KWS;
|
|
using UnityEngine;
|
|
|
|
namespace ShiningGames.UFS2
|
|
{
|
|
public class TestAtachment : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private float _GravityPwr;
|
|
|
|
[SerializeField]
|
|
private bool _IsInWater;
|
|
|
|
private SpringJoint _Joint;
|
|
|
|
private Rigidbody _RBody;
|
|
|
|
private float _VelocityMagnitude;
|
|
|
|
private float speed;
|
|
|
|
private Vector3 lastPosition;
|
|
|
|
public float VelocityMagnitude => _RBody.velocity.magnitude;
|
|
|
|
public float Speed => speed;
|
|
|
|
public event Action OnEnterToWater;
|
|
|
|
public event Action OnExitFromWater;
|
|
|
|
private void Start()
|
|
{
|
|
_RBody = GetComponent<Rigidbody>();
|
|
_Joint = GetComponent<SpringJoint>();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
UpdateHandler();
|
|
_VelocityMagnitude = _RBody.velocity.magnitude;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
speed = (base.transform.position - lastPosition).magnitude / Time.deltaTime;
|
|
lastPosition = base.transform.position;
|
|
}
|
|
|
|
public void SetVelocity(Vector3 velocity)
|
|
{
|
|
_RBody.velocity = velocity;
|
|
}
|
|
|
|
private void UpdateHandler()
|
|
{
|
|
if (WaterSystem.IsSphereUnderWater(base.transform.position, 0.05f))
|
|
{
|
|
if (!_IsInWater)
|
|
{
|
|
_IsInWater = true;
|
|
this.OnEnterToWater?.Invoke();
|
|
}
|
|
_RBody.velocity *= 0.91f;
|
|
_RBody.AddForce(Vector3.up * _GravityPwr * _RBody.mass);
|
|
}
|
|
else if (_IsInWater)
|
|
{
|
|
_IsInWater = false;
|
|
this.OnExitFromWater?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void AddForce(Vector3 force)
|
|
{
|
|
_RBody.AddForce(force);
|
|
}
|
|
|
|
public void SetJointLength(float length)
|
|
{
|
|
_Joint.minDistance = 0f;
|
|
_Joint.maxDistance = length;
|
|
}
|
|
}
|
|
}
|