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 /
avatar image
0
Question by Heukhyeon · Dec 27, 2017 at 07:03 AM · rotationmovementrigidbody2d

How can I make my character walk along the slope?

Moving a RigidBody-equipped character from the ground, or "climbing" the slope did not have much of a problem. Exactly, there was not a big problem with the Y-axis velocity value being 0 when you climbed the surface.

alt text

The problem is that if you go down the slope, CollisionExit and CollisionEnter are constantly called to see if the angle between the surface and the ground is different and the player does not seem to make the right move.

alt text

Here is the code related to my movement and rotation. What parts can be modified to fix the problem when moving on the surface?

 int Y_Rot=0;//The Y value of EulerAngles. 0 == right, 180 = left
 float Speed=3;
 Transform floor;

 private void OnCollisionEnter2D(Collision2D collision) => floor = collision.transform;
 private void OnCollisionStay2D(Collision2D collision)=> floor = collision.transform;
 private void OnCollisionExit2D(Collision2D collision)=> floor = null;
 private void FixedUpdate()
 {
     Vector3 look = transform.eulerAngles;
 //If the character is standing on the ground
     if (floor != null)
     {
     //If the character is looking to the left, inverse the current ground landing to get the EulerAngle value.
         look = (look.y == 0 ? floor.rotation : Quaternion.Inverse(floor.rotation)).eulerAngles;
         look.y = Y_Rot;

     //Set the velocity value to 0 to prevent the character from falling when the character is facing up. (The character could not climb the slope when annotating that part)
         if(Rigidbody.velocity.y < 0 && transform.right.y >= 0)Rigidbody.velocity = Vector2.zero;
     }

 //When the character is floating in the air
     else
     {
         look.y = Y_Rot;
         look.z = 0;
     }
     transform.rotation = Quaternion.Euler(look);
 }


 //basedir: Indicates the direction of movement of the x axis when the character is on the ground.
 public void SetMoveDir(Vector3 basedir)
 {
     Vector2 dir = basedir;
 //If there is a movement direction, adjust the Y-axis rotation value to match that direction.
     if(dir.x!=0) Y_Rot = dir.x == 1 ? 0 : 180;

 //If it touches the ground surface, it moves to fit the ground surface. If it does not touch the ground surface, it moves to the default direction.
     if (floor != null)
     {
         if (dir.x != 0)
         {
             FixedUpdate(); //Manually invoke FixedUpdate to rotate to the angle of the surface.
             dir = transform.right.normalized * Time.deltaTime;
             dir.x *= Speed;
             Rigidbody.position += dir;
         }
     }
     else Rigidbody.position += dir * Time.deltaTime;
 }

1.gif (325.6 kB)
2.gif (281.0 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

· Add your reply
  • Sort: 
avatar image
0

Answer by Heukhyeon · Dec 27, 2017 at 01:04 PM

I got the answer. We corrected the error by reading the Bound value of the Renderer rather than getting the bottom from the collision, and calculating the height and processing it by directly racasting it.

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

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

129 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

Related Questions

Colliding players spin out of control and rotation is local. Need help! [C#] 0 Answers

How can i edit this code to rotate my player to fixed angles when a key (or a combo of keys) is pressed? 1 Answer

How to make cars rotate towards a waypoint while turning? 0 Answers

In unity 2D c# how to rotate an object like geometry dash? 1 Answer

Help with translation and rotation on a rigid body 2D 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