- Home /
What is wrong about this RaycastHit Event???
The raycast event is not firing. Here is the script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FixedJointCreation : MonoBehaviour {
public GameObject foot;
public bool CreateJoint = false;
public float raycastDistance = 100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
int layermask = 1 << 8;
layermask = ~layermask;
RaycastHit hit;
if ((Physics.Raycast(foot.transform.position, -foot.transform.TransformDirection(Vector3.up), out hit, raycastDistance, layermask)) && (CreateJoint == false))
{
Debug.DrawRay(foot.transform.position, foot.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
FixedJoint fixedjoint = foot.AddComponent(typeof(FixedJoint)) as FixedJoint;
fixedjoint.breakForce = 3;
fixedjoint.breakTorque = 10;
fixedjoint.anchor = foot.transform.position;
Debug.Log("RaycastFired");
CreateJoint = true;
}
else
{
CreateJoint = false;
}
}
}
It isn't debugging "RaycastFired" and isn't drawing a ray either. The raycast isn't even activated, even when it should be. I tried extreme values for the distance too, and it didn't fix it.
The "RaycastFired" log message and drawing of the debug ray will only happen if the raycast hits something ( Physics.Raycast()
only returns true if it does). You use a layermask, it is possible that you don't hit anything that is on the specified layer.
$$anonymous$$ove the Debug.DrawRay()
just before you do the raycast with (a specific distance), or possibly add it to the else branch, and draw it with a different color (to show that this is the raycast that didn't hit anything).
is CreateJoints false in the inspector? is the script active? is the gameobject active?
Answer by UnityCoach · Aug 02, 2018 at 07:55 AM
Try this :
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 = 100f;
int layermask = -5; // layer mask can be declared here, -5 = everything but Ignore Raycast layer
RaycastHit hit; // same thing, rayCastHit can be declared here
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
if (Physics.Raycast(transform.position, -transform.up, out hit, raycastDistance, layermask))
{
FixedJoint fixedjoint = gameObject.AddComponent<FixedJoint>();
fixedjoint.breakForce = 3;
fixedjoint.breakTorque = 10;
fixedjoint.anchor = transform.position;
Debug.Log("RaycastFired");
enabled = false; // you can simply disable the component once it's created the joint
}
}
}
The changes are all commented, but the most important (to your issue) is that Debug.DrawRay()
is outside the if(Physics.Raycast(...))
condition so that it is always drawn.
The comments give very good suggestions, I recommend you try UnityCoach's script, it should work.
@Harinezumi , you're right. I usually do what you suggested (changing the color of the Debug.DrawRay whether the Raycast hits or not), but there, I believe the script should be disabled as soon as it hits anything, as its purpose is to create a FixedJoint.
Your answer
Follow this Question
Related Questions
Having a RaycastHit event create a component? 1 Answer
Cube clips and falls through wall when held 0 Answers
Trouble with checking Raycast 1 Answer
Raycast and colliders problem. Corners of two colliders 0 Answers
Held cube clips through wall 0 Answers