• 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 /
  • Help Room /
avatar image
0
Question by MadPropz101 · Nov 11, 2018 at 02:53 AM · 2d rotation

Rotate 2D character based on terrain angle?

Hello everyone,

In my 2D game i need to make it so that my character faces the appropriate angle based on the slope of the terrain that he is standing on. I heard that this can be done using Raycast but i have never used it before and am not too familiar with vectors, can somebody give me an example on how to implement this feature? Is there a way to implement this in a manor that is not too complicated?

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
0

Answer by CDAXRC · Nov 11, 2018 at 09:51 AM

 void OnCollisionEnter2D(Collision2D collision)
 {
     Vector2 normal = collision.contacts[0].normal;

 }

You need to define normal

Comment
Add comment · Show 2 · 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 MadPropz101 · Nov 11, 2018 at 02:35 PM 0
Share

I understand that, but what then? I can never find an explanation of the whole method. How do you use this normal to rotate the character, and how/why does it work?

avatar image CDAXRC MadPropz101 · Nov 11, 2018 at 05:41 PM 0
Share

Try this code.

   public Rigidbody2D rb;
   public  float downForce = 60;
   public  float moveForce = 50;
   public  bool grounded;
     Vector2 normal;
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody2D>();
         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
     }
     
     // Update is called once per frame
     void Update () {
 
        
         transform.up = normal; // This set transform to desired rotation.
         Debug.DrawRay(transform.position, normal * 100, Color.red);
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
         {
             rb.AddForce(transform.right * moveForce);
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
         {
             rb.AddForce(-transform.right * moveForce);
         }
   
         if (grounded == true)
         {
             rb.AddForce(-transform.up * downForce);
         }
       
     }
     void OnCollisionEnter2D(Collision2D collision)
     {
         normal = collision.contacts[0].normal;
 
       
         if (collision.gameObject != null)
         {
             grounded = true;
         }
     }
     private void OnCollisionExit2D(Collision2D collision)
     {   
        
         
             grounded = false;
         
     }

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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

Related Questions

Enemy Ai Rotation not working properly 0 Answers

2D Rotation based on 2 points, and the math is returning wrong values. 0 Answers

How to make a 2D rigidbody vertically when input is via the gyroscope 0 Answers

[Normal Map on 2D Graphics] I NEED HELP! 0 Answers

How to Transform.Rotate Around with the rotation of the controller right stick 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