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 kidocty · May 03, 2017 at 11:41 AM · rigidbodyterrainalignmentalign two objects

Rotating a player to match terrain slope

Hello everyone,

I know this is a common question but unfortunately the common solutions aren't working. I'm building a snowboarding game and I'm trying to align my player to match the slope of my terrain object. I have a Rigidbody on my object to handle the physics. I have rotation locked on all three axis so that the object just slides down the slope. However this means that the object doesn't line up properly with the terrain and causes the front end to stick up as shown in the picture.

alt text

I've tried using Raycasts from transform.position as well as from two empty game objects, one on the front and the back of the board and even trying to rotate the rigidbody itself. I don't know whether to try removing the rigidbody and generating the physics myself.

Does anyone have any ideas on what else I can try?

screen-shot-2017-05-01-at-213723.png (297.3 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Fanttum · May 17, 2017 at 03:55 PM

You'll want to find the normal of the ground below and then make your body rotate based on that. Here is some simple code.

     RaycastHit hit;            
     if(Physics.SphereCast(transform.position, 0.5f, -(transform.up), out hit, yourDistenceToGroundYouWant, yourGroundLayers)) {  
      transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, hit2.normal)  
                 }

I use a Sphere cast, but I think a normal ray cast will work too. If you need more help with the vector math let me know! Hope this helps.

Comment
Add comment · Show 3 · 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 TheDeveloper10 · May 28, 2017 at 02:17 PM 0
Share

How Can You Do the code for 3D Objects ? It's working for "Rotation X". - Thanks!

avatar image Ivoryjw · Jan 20, 2019 at 04:14 AM 0
Share

Thanks for the code!

avatar image sebeman3 · Jul 11, 2019 at 01:48 PM 0
Share

What is the "hit.2"?

Is that another RayCastHit?

avatar image
0

Answer by Piyush_Pandey · May 04, 2017 at 06:38 AM

Hope it helps.

00) Create a child and attach all the physics components you want the player to have. Add a sphere collider to it so that it can roll down the mountain smoothly along with your constraints.

01) Now the mesh of your fat guy on the ironing board is parent of this.

10) Find the angle of the slope your player is in.

11) Rotate the player according to the slope smoothly with Vector3.SmoothDamp()

Comment
Add comment · Show 1 · 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 kidocty · May 17, 2017 at 02:44 PM 0
Share

Hi Piyush, Thanks for your answer. I've already tried something similar to this and it didn't work. Unfortunately the way you have the parent/child set up the sphere collider rolls away without the mesh. The collider always needs to be on the parent

avatar image
0

Answer by $$anonymous$$ · May 29, 2017 at 06:34 AM

I recently found a solution to a similar problem where I wanted to get the up vector for a golfer game object where the left foot, right foot, and club head would always be touching the ground.

             Transform leftFoot = golfer.transform.Find("Game_engine/Root/pelvis/thigh_l/calf_l/foot_l");
     Transform rightFoot = golfer.transform.Find("Game_engine/Root/pelvis/thigh_r/calf_r/foot_r");

     RaycastHit lf;
     RaycastHit rf;
     RaycastHit gb;

     Physics.Raycast(leftFoot.position + Vector3.up, Vector3.down, out lf);
     Physics.Raycast(rightFoot.position + Vector3.up, Vector3.down, out rf);
     Physics.Raycast(golfBall.position + Vector3.up, Vector3.down, out gb);
     Vector3 side1 = lf.point - gb.point;
     Vector3 side2 = rf.point - gb.point;
     Vector3 perpUp = Vector3.Cross (side2, side1).normalized;

This works if you have three points as defined in the Unity Documentation on Vector3.Cross. This may help you if you can find another point on the plane of your snowboard.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate a player to match the terrain slope? 0 Answers

After parent change, rigidbody falls through terrain 0 Answers

Walls not moving in 1 Answer

Colliding with Terrain and Destroying it 1 Answer

How to make a deform on a terrain with the points on a collision of rigidbody 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