- Home /
Having a RaycastHit event create a component?
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;
}
}
}
Answer by bennett_apps · Aug 04, 2018 at 11:59 PM
Typically, you would have an animation of the feet moving, but the feet wouldn't actually move the object. You use physics to move the entire object at once...
Your answer
![](https://koobas.hobune.stream/wayback/20220612172110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast problem 1 Answer
What is wrong about this RaycastHit Event??? 1 Answer
Trouble with checking Raycast 1 Answer