• 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
Question by Baskyn · Mar 04, 2013 at 06:29 AM · rotationphysicsraycastcarvehicle

Raycast align with ground normal

I felt this was a better place to ask this, rather than the forums. Ok, so I have a car that I can move around and I need it to align itself to the ground normals for a Mario Kart/Little Big Planet Kart type of physics. The script I have has some problems. Whenever it "aligns" me to the ground normals it sets my Y rotation to the specific rotation every time. It also doesn't align me often enough. It usually only works when I go up and down slopes. It is in the Update, so that's not the problem. Here is my script so far:

     var hit : RaycastHit;
     var castRot = Vector3(transform.position.x,transform.position.y-.50,transform.position.z);
     if (Physics.Raycast (castRot, -transform.up, hit)) {
         transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
     }

Does anybody have an idea of what is wrong?

Comment
aldonaletto
DanishFsh

People who like this

2 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 robertbu · Mar 04, 2013 at 06:47 AM 0
Share

Two ideas out of my head without testing, and neither of which may work. The first is to keep track of the previous normal. Then you generate a rotation by calculating the FromToRotation() using the previous and current normals. Then you multiply that rotation by the current rotation of the car. The car needs to start aligned with the initial value of prevNormal;

 Quaternion q = Quaternion.FromToRotation(prevNormal, hit.normal);
 prevNormal = hit.normal;
 transform.rotation = q * transform.roation;

Another possibility is to separate the two rotation into different game object. Have the empty parent game object do the normal alignment and then have the car do it turning through transform.localRotation.

avatar image whydoidoit · Mar 04, 2013 at 08:58 AM 0
Share

You could also try setting transform.up = hit.normal.

What you don't want to do is set the rotation to just that FromTo as @robertbu says - you would want to modify the existing rotation. transform.up = hit.normal does try to do that.

avatar image Baskyn · Mar 04, 2013 at 10:04 AM 0
Share

Ok, is this right?

     var hit : RaycastHit;
     var castRot = Vector3(transform.position.x,transform.position.y-.50,transform.position.z);
     
     if (Physics.Raycast (castRot, -transform.up, hit)) {
         prevNormal = hit.normal;
         Quaternion q = Quaternion.FromToRotation(prevNormal, hit.normal);
         transform.rotation = q * transform.rotation;

It says ';' is expected on the line, "Quaternion q = Quaternion.FromToRotation(prevNormal, hit.normal); ". Can you not declare Quaternion variables like this?

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Mar 04, 2013 at 11:34 AM

My answer to this question shows a script that aligns a CharacterController vehicle to the ground (aligns its transform, not the CharacterController: the CC is always upright). This script works fine for regular terrains, but may fail when the terrain steepness is too high, specially if it turns upside down. For a more generic case that can handle vertical or upside down surfaces, take a look at my answer to this question - it uses a rigidbody instead of a CharacterController, which may be more convenient for a vehicle.

Comment
robertbu
Loius

People who like this

2 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 Baskyn · Mar 04, 2013 at 12:00 PM 0
Share

So far I've tried the first one and it works REALLY well. It will need a bit of tweeking for my own purpose, but it's great! Since you said the other one would be better for rigid bodies, I'll try it out tomorrow and get back to you!

I had seen the first one before, but never tried it. Pretty dumb of me. I have to thank you, as I've been trying to make this work for weeks!

Also, I've thought about using a character controller for a vehicle. What do you think about them for a Kart game like I'm trying out?

avatar image

Answer by Ender Davis · May 02, 2013 at 05:02 AM

I found transform.up-=(transform.up-hit.normal)*.1; to work really well.

Comment
refardeon

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Raycast Car-Vehicle Physics 1 Answer

How do i make my Raycast Car not slip at all? 0 Answers

hello's i needs help on camera movement 1 Answer

Vehicle engine RPM without wheelcolliders? 1 Answer

How to make a car not slow down while turning? 2 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