Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

weird Skin width problem with raycast collision 0 Answers

Raycast occasionally won't hit colliders on Mecanim animated characters (but hits perfectly if Animator is disabled) 1 Answer

CircleCast hits emitter with Collider2D 0 Answers

How can I change the Unity 5 UI hitbox/collider to fit my complex image? 1 Answer

Raycast exit point of collider 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges