I’m trying to make my procedural animation spider work by its self and not with the controller. well it does work by itself but I need one thing done all i need is my spider to check which way it is facing bc i want to get rid of the public float move direction and make it so it checks which way the spider is facing so i can replace the void update if movedirection to whatever you guys replace it with.
cheers
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class legmover : MonoBehaviour
{
public Vector3 stepnormal;
public Transform raycastpoint;
public Transform target;
public Vector3 restingposition;
public LayerMask mask;
public Vector3 newposition;
public Transform steppingpoint;
public bool leggrounded;
public GameObject player;
public float offset;
public float movedistance;
public static int currentmovevalue = 1;
public int movevalue;
public float speed = 10f;
public legmover otherleg;
public bool hasmoved;
public bool moving;
public bool movingdown;
public float movedirection
{
get
{
return controller.instance.movey;
}
}
public bool walkingbackwards = false;
void Start()
{
restingposition = target.position;
steppingpoint.position = new Vector3(restingposition.x + offset, restingposition.y, restingposition.z);
}
void Update()
{
if(movedirection > 0 && walkingbackwards != true)
{
walkingbackwards = true;
offset = -0.6f;
steppingpoint.localPosition = new Vector3(steppingpoint.localPosition.x + offset, steppingpoint.localPosition.y, steppingpoint.localPosition.z);
}
else if(movedirection < 0 && walkingbackwards != false)
{
walkingbackwards = false;
offset = 0.6f;
steppingpoint.localPosition = new Vector3(steppingpoint.localPosition.x + offset, steppingpoint.localPosition.y, steppingpoint.localPosition.z);
}
newposition = calculatepoint(steppingpoint.position);
if(Vector3.Distance(restingposition, newposition) > movedistance || moving && leggrounded)
{
step(newposition);
}
updateik();
}
public Vector3 calculatepoint(Vector3 position)
{
Vector3 dir = position - raycastpoint.position;
RaycastHit hit;
if (Physics.SphereCast(raycastpoint.position, 0.5f, dir, out hit, 5f, mask))
{
stepnormal = hit.normal;
position = hit.point;
leggrounded = true;
}
else
{
stepnormal = Vector3.zero;
position = restingposition;
leggrounded = false;
}
return position;
}
public void step(Vector3 position)
{
if(currentmovevalue == movevalue)
{
leggrounded = false;
hasmoved = false;
moving = true;
target.position = Vector3.MoveTowards(target.position, position + Vector3.up, speed * Time.deltaTime);
restingposition = Vector3.MoveTowards(target.position, position + Vector3.up, speed * Time.deltaTime);
if(target.position == position + Vector3.up)
{
movingdown = true;
}
if (movingdown == true)
{
target.position = Vector3.MoveTowards(target.position, position, speed * Time.deltaTime);
restingposition = Vector3.MoveTowards(target.position, position, speed * Time.deltaTime);
}
if(target.position == position)
{
leggrounded = true;
hasmoved = true;
moving = false;
movingdown = false;
if (currentmovevalue == movevalue && otherleg.hasmoved == true)
{
currentmovevalue = currentmovevalue * -1 + 3;
}
}
}
}
public void updateik()
{
target.position = restingposition;
}
}
and here is the controller script as well if you need it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controller : MonoBehaviour
{
public int[] referenceleg;
public int[] referenceleg2;
public legmover[] legs;
public AnimationCurve senscurve;
public float desiredsurfdst = -1f;
public bool grounded;
private Rigidbody rb;
public float speed;
public float angspeed;
private float movex;
public float movey;
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
}
public static controller instance;
private void Awake()
{
instance = this;
}
void Update()
{
if(movey != 0)
{
Vector3 movement = transform.forward * movey * speed * Time.deltaTime;
rb.velocity = movement;
}
transform.Rotate(0, movex * angspeed * Time.deltaTime, 0);
calcOrientation();
}
public void calcOrientation()
{
Vector3 up = Vector3.zero;
Vector3 point, a, b, c;
float avagsurfDst = 0;
grounded = false;
for (int i = 0; i < legs.Length; i++)
{
point = legs*.newposition;*
a = ((legs[referenceleg*].newposition) - point).normalized;*
b = ((legs[referenceleg2*].newposition) - point).normalized;*
avagsurfDst += transform.InverseTransformPoint(point).y;
c = Vector3.Cross(a, b) * -1;
Debug.DrawRay(point, a, Color.red, 0);
Debug.DrawRay(point, b, Color.blue, 0);
Debug.DrawRay(point, c, Color.green, 0);
up += c * senscurve.Evaluate(c.magnitude) + (legs_.stepnormal == Vector3.zero ? transform.forward : legs*.stepnormal);_
_grounded |= legs.leggrounded;*_
}
up /= legs.Length;
avagsurfDst /= legs.Length;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, up), up), 20f * Time.deltaTime);
if (grounded)
{
transform.Translate(0, -(-avagsurfDst + desiredsurfdst), 0, Space.Self);
}
/*
else
{
transform.Translate(0, -10 * Time.deltaTime, 0, Space.Self);
}
*/
}
}