I am creating a physics-based character control system and I plan to have the movement completely physics. I have a walking animation running, but the feet are not planting into the ground and pushing the character forward that much, and it is very uncoordinated. I plan to have the feet create FixedJoints with a small break force/torque, so the animation will naturally break it. How could I script this? I tried a script, but it isn’t working. Here it is. You may edit this, or write a completely new one. Thanks!
(The script goes in to each foot)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FixedJointCreation : MonoBehaviour
{
// public GameObject foot; // you don't need this, put the script on the object
// public bool CreateJoint = false; // you don't need this
public float raycastDistance;
int layermask = 1 << 8; // layer mask can be declared here, -5 = everything but Ignore Raycast layer
public bool CreateJoint = false;
private void Start()
{
Debug.Log("FixedJointCreation Started");
}
void FixedUpdate() // I believe creating a Joint would be better in FixedUpdate
{
// use transform.up gives the same as transform.TransformDirection(Vector3.up)
Debug.DrawRay(transform.position, transform.forward * raycastDistance, Color.yellow);
// YOU ARE NOT USING THE SAME VECTOR FOR RAYCASTING AND DEBUG, DEBUG USES forward, RAYCASTING uses -up
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.up, out hit, raycastDistance) && (CreateJoint == false))
{
FixedJoint fixedjoint = gameObject.AddComponent<FixedJoint>();
fixedjoint.breakForce = 3;
fixedjoint.breakTorque = 10;
fixedjoint.anchor = transform.position;
Debug.Log("RaycastFired");
Debug.DrawRay(transform.position, transform.forward * raycastDistance, Color.green);
CreateJoint = true;
}
else
{
CreateJoint = false;
}
}
}