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 JokeOfAllTrades · May 19, 2018 at 07:13 AM · rotationcollider

How can I rotate a player proportionally over a mesh?

I’m making 3PS/FPS where they player controls a tank that needs to stay level with whatever surface they are on. Most surfaces are going to be 3d like a sphere or like the inside of a cube (so not like a terrain).

Below is a function I use in the player controller script to ensure that the player is always level with the surface they are on.

     void LevelTankOnMove()
     {
         float rotationAngle;
         Vector3 rotationDirection;
         Vector3 center = transform.position + transform.TransformVector(0, down, 0);
 
         if (Physics.Raycast(center, -transform.up, out hitInfo))
         {
             Quaternion.FromToRotation(transform.up, hitInfo.normal).ToAngleAxis
                 (out rotationAngle, out rotationDirection);
             transform.RotateAround(center, rotationDirection, rotationAngle);
         }
     }
 

It works great if the player is on a spherical collider. However, if the player is on a more conventional mesh, they rotate instantly when crossing over the seam between surfaces of the mesh (when the center of the tank crosses the seam). alt text

alt text

What I’d like to do is have the tank rotate over the seam in proportion to the amount of the tank that’s over the seam (I'll post pictures in the comments).

Conceptually, I’d project the shape of the player’s collider onto the surface they are on and get a scan of lines, vertices, and other features beneath them that I could use to orient the player. I can do this with raycasts, but I have a feeling that there is an easier way of doing it. Unfortunately, I’m still pretty new to Unity, so I don’t know what part of the API might address this problem, or even what to google (which I have done plenty of).

Any ideas?

after.jpg (18.1 kB)
before.jpg (13.9 kB)
Comment
Add comment · Show 3
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 JokeOfAllTrades · May 19, 2018 at 07:16 AM 0
Share

alt text

roll1.jpg (15.9 kB)
avatar image JokeOfAllTrades · May 19, 2018 at 07:16 AM 0
Share

alt text

roll2.jpg (18.6 kB)
avatar image JokeOfAllTrades · May 19, 2018 at 07:17 AM 0
Share

alt text

roll3.png (51.0 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kishotta · May 19, 2018 at 05:43 PM

One possible solution would be to cast several rays "down" (probably best to do one in the center and in a ring near the edge of the base) and record their hit normals. Then rotate the tank to align with the average normal. This way, if 3/4 rays hit a flat surface (normal is "up"), and 1/4 hits a 45 degree angle, the tanks rotation will be 11 degrees (you'll probably want to use more than 4 rays though).

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 JokeOfAllTrades · Jul 14, 2018 at 07:25 AM 0
Share

I was hoping to find some way of using a navmesh to solve this problem as casting rays down would have caused problems when the tank moved over the edge of a cube i.e. the rays would never collide with anything. I fixed this by inserting a check for this situation which thenhas rays cast out from the center.

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

126 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

Related Questions

Common 2D Collider flip problem 4 Answers

Physic Collider Test Rotation 0 Answers

Internal collisions 1 Answer

Enemy AI capsule collider tilts when moving 1 Answer

How to keep the orientation of an object unchanged when applying forces on its sphere collider? 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