Raycast not getting moving object

8258862--1081167--upload_2022-7-6_15-59-40.png


I am trying to make a grappling hook in Unity. it works fine on stationary objects, but the raycast doesn’t work with objects that are animated, have scripts moving them, or have a rigidbody. When I try to view their properties during run-time using a breakpoint, it says all the components are “depreciated”. I have tried getting the object from the raycast hit, I have tried getting the object from a parent object and a child object, I have tried getting an object using its components, and I have tried getting the object with GetComponent<> and FindComponent<>. So far nothing has worked.


Can you share your raycasting code and expanded views of your inspectors?

As for all those deprecated errors, it’s referring to the shortcut properties that were in every MonoBehaviour. Nothing to worry about.

    public GameObject grappleObject;
    public Vector3 grapplePoint;
    public Vector3 grappleOffset;
    private SpringJoint joint;
    public GameObject grappleMarker;

//update
        RaycastHit hit;
        if (Physics.Raycast(cam.transform.Find("rayCastShooter").position, cam.transform.forward, out hit, range))
        {
            grappleObject = hit.collider.gameObject;
            grappleMarker.transform.SetParent(grappleObject.transform);
            grappleObject = grappleMarker.transform.parent.gameObject;
            grapplePoint = hit.collider.transform.position;
            grappleOffset = hit.point - hit.collider.transform.position;
            joint = transform.parent.transform.parent.gameObject.AddComponent<SpringJoint>();
            joint.autoConfigureConnectedAnchor = false;
            joint.spring = 2.0f;
            joint.damper = 3f;
        }

8297478--1087935--upload_2022-7-20_16-3-8.png

Raycast is working and you are doing something wrong.

Try with something simple, just make raycast from point in world space to particular direction, move your object to make the intersection with the ray.

1 Like

That may be an observed characteristic but that is not the problem.

Remember in Unity only ONE THING moves at a time, the thing you are moving.

Even things with Rigidbodies or Animations are absolutely positively 100% FROZEN in place except when the piece of code (Physic system or animation system) moves it.

My guess is one of the standard raycasting fails:

  • you are casting from the wrong spot
  • you are casting in the wrong direction
  • the object you think you can hit doesn’t have a collider
  • you are mis-calling Raycast (see below)
  • you are starting from within the object
  • you are not casting far enough
  • you have a layer misconfigured

etc.

Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:

https://discussions.unity.com/t/758115/8