147 lines
3.7 KiB
C#
147 lines
3.7 KiB
C#
using NBC;
|
|
using UnityEngine;
|
|
|
|
namespace PhysicsTools
|
|
{
|
|
public class RopeTest : MonoBehaviour
|
|
{
|
|
public Rope rope;
|
|
|
|
public Transform startPosition;
|
|
|
|
public Transform throwPosition;
|
|
|
|
public Transform ropeStart;
|
|
|
|
public Transform ropeFloat;
|
|
|
|
public Transform ropeBait;
|
|
|
|
public Transform fishingPlayer;
|
|
|
|
public bool updateDistance;
|
|
|
|
public float incDist = 0.1f;
|
|
|
|
public float incDistThrow = 0.5f;
|
|
|
|
public float incDistWater = 0.1f;
|
|
|
|
public float maxLength = 25f;
|
|
|
|
public Vector3 throwDir = Vector3.zero;
|
|
|
|
public float throwForce = 1000f;
|
|
|
|
public float prevDistance = -1f;
|
|
|
|
public float currentDistance = -1f;
|
|
|
|
public float reelInSpeed = 0.01f;
|
|
|
|
[HideInInspector] public float reelInFactor;
|
|
|
|
private bool hitWater;
|
|
|
|
private bool wasThrown;
|
|
|
|
public bool hasFish;
|
|
|
|
private void Start()
|
|
{
|
|
prevDistance = (currentDistance = CalculateDistance());
|
|
ResetBait(true);
|
|
ropeStart.parent = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (hitWater && rope.getLength() < 1f)
|
|
{
|
|
ResetBait();
|
|
return;
|
|
}
|
|
|
|
reelInFactor = 0f;
|
|
if (Input.GetKey(KeyCode.N))
|
|
{
|
|
reelInFactor = reelInSpeed;
|
|
}
|
|
else if (Input.GetKey(KeyCode.M))
|
|
{
|
|
reelInFactor = 0f - reelInSpeed;
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.B))
|
|
{
|
|
ThrowBait();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.V))
|
|
{
|
|
ResetBait(true);
|
|
}
|
|
|
|
ReelIn(reelInFactor);
|
|
if (!hitWater && ropeStart.position.y <= 0f)
|
|
{
|
|
incDist = incDistWater;
|
|
hitWater = true;
|
|
}
|
|
|
|
ropeStart.GetComponent<Rigidbody>().linearDamping = ((!(ropeStart.position.y <= 0f)) ? 0f : 5f);
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
prevDistance = currentDistance;
|
|
currentDistance = CalculateDistance();
|
|
if (updateDistance && (!hitWater || hasFish) && rope.getLength() < maxLength && wasThrown &&
|
|
rope.getLength() < currentDistance + incDist)
|
|
{
|
|
rope.changeLength(currentDistance - rope.getLength() + incDist);
|
|
}
|
|
|
|
Debug.Log("currentDistance: " + currentDistance + " rope.getLength(): " + rope.getLength());
|
|
}
|
|
|
|
public void ReelIn(float reelIn)
|
|
{
|
|
if (reelIn < 0f && rope.getLength() <= rope.segPropertiesCylinder.length)
|
|
{
|
|
reelIn = 0f;
|
|
}
|
|
|
|
rope.rate = reelIn;
|
|
}
|
|
|
|
public float CalculateDistance()
|
|
{
|
|
return Vector3.Distance(ropeStart.position, ropeFloat.position);
|
|
}
|
|
|
|
public void ThrowBait()
|
|
{
|
|
Debug.Log("ThrowBait");
|
|
incDist = incDistThrow;
|
|
ropeStart.position = throwPosition.position;
|
|
rope.regenerateRope(true);
|
|
ropeStart.GetComponent<Rigidbody>().AddForce((fishingPlayer.forward + throwDir).normalized * throwForce);
|
|
Debug.DrawLine(ropeStart.position,
|
|
ropeStart.position + (fishingPlayer.forward + throwDir).normalized * throwForce, Color.yellow, 5f);
|
|
wasThrown = true;
|
|
}
|
|
|
|
public void ResetBait(bool quick = false)
|
|
{
|
|
if (quick)
|
|
{
|
|
ropeStart.position = startPosition.position;
|
|
rope.regenerateRope(true);
|
|
}
|
|
|
|
hitWater = false;
|
|
wasThrown = false;
|
|
}
|
|
}
|
|
} |