I am following this tutorial:
I have tried to translate the code to C# to the best of my ability and it does not work.
Here are the scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Fabrik : MonoBehaviour
{
public List<FabrikLimb> limbs = new List<FabrikLimb>();
public Transform target;
private float totalLength = 0.0f;
private void Start()
{
CalculateLengths();
}
private void Update()
{
if (InRange())
{
Solve();
}
}
void CalculateLengths ()
{
for(int i = 0; i < limbs.Count; i++)
{
limbs[i].length = Vector3.Distance(limbs[i].inboard.position, limbs[i].outboard.position);
totalLength += limbs[i].length;
}
}
bool InRange()
{
float distToTarget = Vector3.Distance(limbs[0].inboard.position, target.position);
if (totalLength > distToTarget)
{
return true;
}
return false;
}
void Solve ()
{
FinalToRoot();
RootToFinal();
}
void FinalToRoot ()
{
Vector3 currentGoal = target.position;
FabrikLimb currentLimb = limbs[limbs.Count - 1];
while(currentLimb != null)
{
currentLimb.inboard.rotation = Quaternion.FromToRotation(Vector3.up, currentGoal - currentLimb.inboard.position);
currentLimb.outboard.position = currentGoal;
currentGoal = currentLimb.inboard.position;
if(currentLimb.inboardLimb != null)
{
currentLimb = currentLimb.inboardLimb.GetComponent<FabrikLimb>();
} else
{
currentLimb = null;
}
}
}
void RootToFinal ()
{
Vector3 currentInboardPosition = limbs[0].inboard.position;
FabrikLimb currentLimb = limbs[0];
while(currentLimb != null)
{
currentLimb.inboard.position = currentInboardPosition;
currentInboardPosition = currentLimb.outboard.position;
if(currentLimb.outboardLimb != null)
{
currentLimb = currentLimb.outboardLimb.GetComponent<FabrikLimb>();
} else
{
currentLimb = null;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class FabrikLimb : MonoBehaviour
{
public Transform inboard, outboard;
public Transform inboardLimb, outboardLimb;
public float length = 0.0f;
}
And here is the result:
And here is some extra screenshots if it helps:
I have tried recreating the code to C# to the best of my ability but it still is not working. Does anyone know what I need to fix?