• 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 Mystify · Apr 28, 2016 at 05:18 AM · 2d-platformer

Getting character to rotate to follow terraina

I'm making a 2d platformer, and I want to be able to run up a slope and then up the wall. To do this, I am able to find the normal of the ground, and use it to determine which way I should orient the character. My problem comes when trying to rotate the character to this angle. It is a rigidbody being moved by forces. At first, I was able to rotate its transform to the desired angle, but that would cause it to intersect with the wall I am trying to run on. I then tried to rotate its transform using the bottom of the feet as the pivot, but that seems to mess up the characters motion. I then tried to rotate the rigidbody using physics. Setting the angular velocity myself didn't work right, I would set an angular velocity of, say, -.7, and it would turn to the wrong angle with an angular velocity of 506. It would stabalize at a certain angle, but it would be leaning into the slope, rather than away from it. I tried the rigidbody.MoveRotation, but the angle it ended up with was strange. For instance, moving it to 0 ended up with an angle of -17.74 degrees. This persisted even if I had the character jump, so I am in midair away from any obstacles. If I selected isKinemetic on my rigidbody, then it just rotates to 0.

I feel like this should be simple to accomplish, but every technique I have tried fails to work. What is the proper way to do this?

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 N1warhead · Apr 28, 2016 at 07:26 AM

From what I'm understanding, which without code it's just guesstimating. But it sounds like the player is rotating with the Proper angle on your ground pieces right? But if you jump, it's hitting the Wall pieces and then making the player straight 90 degrees or at the angle of anything the ray is hitting?

If so, you can do something like this and it should work. (Not Tested - but compiled with no errors).

     void Update(){
         RaycastHit2D hit = Physics2D.Raycast (transform.position, -Vector2.up,0.1f);        //0.1f = Distance. Change to what you like, if you want infinity, change to Mathf.infinity.
         if (hit.transform.gameObject.tag == "SomeTag") {
             transform.up = hit.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 Mystify · Apr 28, 2016 at 12:06 PM 0
Share

This will rotate the character to the desired angle, but it behaves exactly like my first case - the resultant rotation pushes my characters feet into the ground. This approach also requires me to have the z rotation locked on the rigidbody, and do the adjustment in Update. I don't nessecarily have a problem with that, but it seems like I am fighting against a contraint which is resetting the angle to 0 every tick, which doesn't seem desirable.

avatar image Mystify · Apr 28, 2016 at 02:42 PM 0
Share

To clarify, my issue is not with jumping, its with getting the character to rotate properly while on the ground. I am detecting the desired angle just fine, I have debug logs verifying that the goal angle is what I want. The problem arises when trying to get the character to rotate to that angle. If I use a method to directly change the transform, the rotation will bring it into the ground. RotateAround to pivot around the foot seems like it would solve that, but its alteration of the location of the character messes up the physics. The methods I tried with interacting with the rigidbody have just resulted in various forms of chaos

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter Getting called more than once. 0 Answers

How do i make my player sprite change, when entering a powerup? 1 Answer

Poor quality sprite 2D game 3 Answers

2d Character gets stuck when jumping platforms 2 Answers

How can i use single swipe to run a character continuously. 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