Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 09, 2015 at 09:59 PM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ashky · Aug 31, 2015 at 08:58 PM · 2draycast2d-platformernormalsslope

Aligning a sprite to the ground's normal using raycasts

Hello Unity Community!

I am trying to align a sprite to a slope's normal, in a 2D enviroment that uses physics. I am casting four rays from different points situated near the bottom of the sprite to form sort of a round shape (see the screenshot). alt text

Here's some code:

         RaycastHit2D ray1=Physics2D.Raycast(anchor1.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
         RaycastHit2D ray2=Physics2D.Raycast(anchor2.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
         RaycastHit2D ray3=Physics2D.Raycast(anchor3.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
         RaycastHit2D ray4=Physics2D.Raycast(anchor4.gameObject.transform.position,-Vector2.up,0.25f,whatIsGround);
         
         this.transform.up=(ray1.normal + ray2.normal + ray3.normal + ray4.normal )/4;

This happens in Update(), and works well most of the time. However, sometimes, if there's a sharp angle and I stop the character right in that spot, the sprite literally has seizures.

So, would more rays help to smooth it out? Should they be focused more towards the center and not spread out? How could I prevent the constant jumbling?

Thanks for your time!

143m5vp.png (21.2 kB)
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

  • Sort: 
avatar image
0
Best Answer

Answer by Ashky · Oct 09, 2015 at 09:58 PM

If anyone is interested, using more rays smooths it out and doesn't hit performance at all. I ended up using 8 rays and avoiding terrain with sharp angles under 90 degrees.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

29 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

Related Questions

2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers

Detect slope with raycast2d 1 Answer

Error with 2D Raycast 0 Answers

2D Slope acceleration 1 Answer

Physics.Raycast doesn't hit anything even though Debug.DrawRay works 1 Answer


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