- Home /
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!
Answer by jackmw94 · Dec 01, 2020 at 11:14 PM
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!
Thanks! Exactly what i was looking for. As for the potential issue with wrapping, basically what i have is when you shoot the grapple it creates a Springjoint. After that the continuous raycast checks if there is anything between the original joint and the player, if it detects something it destroys the old joint and creates a new joint where it hit an object. (at least in theory, still working out a few kinks) I'm open to other ideas though, since I'm the one who came up with this solution it's probably not my best option. By automatically, do you mean no physics at all? The grappling/swinging needs to use physics, but the wrapping doesn't need to be exactly the same. (Although the entire environment needs to be grapple-able, so that does make me a little wary of having the entire terrain store points) If you do mean the just the wrapping automatically, could you give me a general idea for how to do that? Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613020914im_/https://answers.unity.com/themes/thub/images/avi.jpg)