How to constantly Raycast (V2)

I tried asking this once, but I don’t think it worked. If this is a duplicate, ignore it. So I’m trying to make a grappling hook for the player, and I’m using a SpringJoint/LineRenderer for it. However, I want it to be able to interact with the environment somewhat, specifically being able to wrap around objects. Anyway my idea is to have a secondary RayCast after the first one, which constantly checks for objects in the way. When it detects one it will create a new joint. There are several problems with this tho. First: How do I get a constant RayCast? I have to create the RayCast inside a method, so can’t really do Update or FixedUpdate. Second: How do i get the RayCast to always check in the direction of the grappling point? Is it possible to make the direction of a RayCast a Vector3? Not really looking for code, just a point in the right direction. Thanks!

Well if you want something to do something continuously once triggered then you have a couple options:

You can perform it inside a coroutine:

private IEnumerator DoThingContinuously()
{
    while (true)
    {
        // perform logic you want to be carried out each frame
        yield return null;
    }
}

Bare in mind you have to start coroutines with:

 var myCoroutine = StartCoroutine(DoThingContinuously());

Here I’m storing a reference to it so I can call StopCoroutine on it when I want it to finish.


Alternatively you can create and destroy new components that perform the action you need:

public class DoThingContinuously : MonoBehaviour
{
    private void Update()
    {
        // perform logic you want to be carried out each frame
    }
}

// then, within the class you're controlling the behaviour from, you can add this component at runtime

private DoThingContinuously instance;

private void StartRunningThing()
{
    instance = gameObject.AddComponent<DoThingContinuously>();
}

private void StopRunningThing()
{
    Destroy(instance);
}

For your second point: yeah the direction of a raycast can happily be a Vector3.

Ray ray = new Ray(transform.position, grapplePosition - transform.position);

The ray constructor takes a direction argument, so if you want it to point towards the grapple point then find the direction as grapple target position minus player position.

Let me know if I’ve misunderstood this bit!

Lastly, if you’re thinking of getting a grapple wire to wrap around objects then I’d strongly advise having this done automatically rather than with raycasts or physics. For example, each grapple-able object having a GrappleBehaviour component that stores points either side of it for the grapple wire to target and handle how the wire wraps around it. If you try go down the route of performing more and more raycasts to find a wire’s direction then it’ll become very complicated and hurt your frame rate. A raycast to find out what you’re hitting is great but additional raycasts to handle wrapping scares me. I might have misunderstood this bit so ignore if I have!