Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Marmadon_ · Jun 19, 2021 at 01:29 AM · raycastraycasthit2d

Raycast hits only one frame

Hi, I'm kinda new to unity so I'm asking ya'll for help on this.


My problem is that transform.rotation only happens on the first frame of the Raycast hitting the ground, but I'm trying to set the object's rotation relative to that surface for as long as the object is grounded.


I'm working with 2D btw

Any help would be appreciated!


void RotateSprite(ref Vector2 moveAmount) { float rayLength = Mathf.Abs(moveAmount.y) + skinWidth;

     for (int i = 0; i < verticalRayCount; i++)
     {
         Vector2 rayOrigin = raycastOrigins.bottomCenter;
         rayOrigin += Vector2.down * (verticalRaySpacing * i + moveAmount.x);
         RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.down, rayLength, collisionMask);

         Debug.DrawRay(rayOrigin, Vector2.down, Color.magenta);

         if (hit && collisions.below)
         {
             float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);

             sprite.transform.rotation = Quaternion.AngleAxis(-slopeAngle, Vector3.forward);

             rayLength = hit.distance;
         }
         else
         {
             sprite.transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
         }
     }
 }

Comment
Add comment · Show 1
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 Lenda0210 · Jun 19, 2021 at 07:21 AM 0
Share

Have you considered looping this through frames, like in the Update() function? You can then check if the object is grounded before calling it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Sinterklaas · Jun 21, 2021 at 01:35 PM

Raycasting functions only check collision at the moment that they're called. They need to be called every frame if you want them to be in effect every frame. Like by putting them in the Update function, which gets called once per frame.


In general this is a good rule-of-thumb: if you want something to happen constantly, you need to run the appropriate code constantly.

Comment
Add comment · Show 5 · 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 Marmadon_ · Jun 21, 2021 at 03:55 PM 0
Share

Yes I thought about that! Couldn't I use some kind of coroutine instead?

avatar image Marmadon_ Marmadon_ · Jun 21, 2021 at 04:13 PM 0
Share

Also it is impossible to pass parameters through update, so I'll need to research on this

avatar image BloodMarked Marmadon_ · Jun 21, 2021 at 04:30 PM 1
Share

you cannot pass things through update. instead, you would create a variable on your script, and write/read to it each update.

avatar image Sinterklaas Marmadon_ · Jun 21, 2021 at 04:32 PM 1
Share

You could use a coroutine, but that function would still run every frame, so you wouldn't really gain anything from doing so.


As for Update() and parameters, you use member variables instead, either declared as public or as private/protected with a [SerializeField] attribute. These variables can then be changed from the inspector, and serve as a stand-in for function parameters. The same goes for other event functions, like Awake(), Start() or FixedUpdate().

avatar image Marmadon_ Sinterklaas · Jun 21, 2021 at 04:55 PM 0
Share

Huge thanks to yall! I'll be looking into this :)

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

170 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

Related Questions

Raycasting in 2D to prevent a player from moving through walls? 1 Answer

RayCast2d Not colliding with objects in other layers 1 Answer

Raycast2D/Line Renderer offset on colliders based on mouse position 0 Answers

Identifier expected on equals symbol 1 Answer

Error with 2D Raycast 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