• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hi317 · Dec 01, 2020 at 06:36 PM · raycastcollision detection

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hi317 · Dec 04, 2020 at 05:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics2D Raycast not detecting wall colliders 1 Answer

Linecast between character and camera doesn't ignoring Characters collider. 2 Answers

How to restrict movement towards a collider. 1 Answer

Raycasting not working on a procedural mesh with a mesh collider. 1 Answer

3d ground for 2d gameplay / side scroller 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges