Here it is. Mainly when I move the rope or it stretches.
Sorry the YouTube embedding seem to not be working for me.
Youtube-Link
The code is extremely messy and a jumble of things I randomly tried. Apologies!
I was going to clean up the code when I got it all working.
Oh by the way running on OSx 12.6 Apple Silicon. too.
Sorry the YouTube embedding seem to not be working for me.
Youtube-Link
The code is extremely messy and a jumble of things I randomly tried. Apologies!
I was going to clean up the code when I got it all working.
Oh by the way running on OSx 12.6 Apple Silicon. too.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class FindClimbObiRope : MonoBehaviour
{
[SerializeField] private GameObject m_Character;
[SerializeField] private Transform handRPos;
[SerializeField] private Transform handLPos;
private int m_ActorParticles = 0;
private int m_MountParticle = 0;
private int m_CurrentParticleOnRopeLength = 0;
private GameObject m_DetectedObject;
private bool ClimbingWithRightHand = true;
private ObiActor ropeComp;
public ObiRope myRope;
public Vector3 foundNearest;
private Vector3 stickItPosition;
private Vector3 oldPosition;
public bool stuckOnRope;
private ObiPathSmoother pathSmoother;
private float currentPositionOnPath; // float 0-1
// Start is called before the first frame update
void Start()
{
ropeComp = myRope.GetComponent<ObiActor>();
pathSmoother = ropeComp.GetComponent<ObiPathSmoother>();
if (ropeComp == null && pathSmoother != null)
{
Debug.Log("No rope found");
}
stuckOnRope = false;
}
void LateUpdate()
{
int indexToRopeParticle = 0;
// scann realtime all the time.
if (Input.GetKeyDown(KeyCode.Tab))
{
if (stuckOnRope == false)
{
indexToRopeParticle = FindClosestRopeParticle();
if (currentPositionOnPath>=0 && currentPositionOnPath<=1) // indexToRopeParticle != 0
{
oldPosition = m_Character.transform.position;
//Debug.DrawLine(m_Character.transform.position, ropeComp.GetParticlePosition(ropeComp.solverIndices[indexToRopeParticle]), Color.magenta);
// Debug.Log("Closest Particle is:" + indexToRopeParticle + " at " + ropeComp.solver.positions[indexToRopeParticle]);
foundNearest = ropeComp.GetParticlePosition(indexToRopeParticle);
// Real world position of particle //
stickItPosition = ropeComp.GetParticlePosition(ropeComp.solverIndices[indexToRopeParticle]);
stuckOnRope = true;
}
else
{
Debug.Log("Nothing found");
}
}
else if (stuckOnRope)
{
m_Character.transform.position = oldPosition;
stuckOnRope = false;
}
}
if (stuckOnRope && (currentPositionOnPath >= 0 && currentPositionOnPath <= 1))
{
if (Input.GetKeyDown(KeyCode.LeftCurlyBracket)) {
currentPositionOnPath -= 0.01f;
if(currentPositionOnPath <0)
{
currentPositionOnPath = 0.0f;
}
}
if (Input.GetKeyDown(KeyCode.RightCurlyBracket))
{
currentPositionOnPath += 0.01f;
if (currentPositionOnPath > 1)
{
currentPositionOnPath = 1.0f;
}
}
// Snap Character to Rope Particle //
StickToRope(currentPositionOnPath);
}
}
private void RightHand()
{
ClimbingWithRightHand = true;
Debug.Log("using the right hand to climb.");
}
private void LeftHand()
{
ClimbingWithRightHand = false;
Debug.Log("using the left hand to climb.");
}
public int FindClosestRopeParticle()
{
//Get the full length of the rope.
m_ActorParticles = ropeComp.solverIndices.Length;
Debug.Log("Finding Closest");
var closestIndexDistance = 8.0f; // max jump distance
int closestIndex = 0;
currentPositionOnPath = -1.0f;
if (handRPos == null || handLPos == null)
{
Debug.LogError("Hand positions haven't been assigned on the rope climb ability.");
return closestIndex;
}
Vector3 handPos = ClimbingWithRightHand ? handRPos.position : handLPos.position;
Debug.Log("Hand pos:" + handLPos.transform.position);
currentPositionOnPath = 100.0f;
float mu = 0;
while (mu < 1)
{
mu += 0.01f;
ObiPathFrame sections = pathSmoother.GetSectionAt(mu);//currentPositionOnPath
var pos = pathSmoother.transform.TransformPoint(sections.position);
float distance = Vector3.Distance(m_Character.transform.position, pos);
if (distance < closestIndexDistance)
{
closestIndexDistance = distance;
//closestIndex = solverIndex;
currentPositionOnPath = mu;
// Debug.Log("current Postion:" + posPathFrame.position+" index:"+ currentPositionOnPath);
}
}
Debug.Log("Closest:" + ropeComp.GetParticlePosition(ropeComp.solverIndices[closestIndex]));
// *** will eventually return currentPositionOnPath ***
return closestIndex;
}
void StickToRope(float ropePosition)
{
pathSmoother = ropeComp.GetComponent<ObiPathSmoother>();
ObiPathFrame section = pathSmoother.GetSectionAt(ropePosition);
m_Character.transform.position = pathSmoother.transform.TransformPoint(section.position);
}
}