Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Patyrn · Apr 02, 2011 at 03:54 AM · vector3quaternionvehiclenormal

Orient a vehicle to the ground and in the direction of its travel.

I have some code that's working well for steering my vehicle towards a waypoint. It orients the vehicle in the direction of its travel and all is good except for one problem: Hills.

I need my vehicle to orient itself in the direction of its travel, but also rotated so its down is aligned with the ground.

Here's my code. I'm not at all sure what to put in the raycast to orient myself to the normal without breaking the rotation that points in the direction I'm going:

Vector3 targetVector = predictivePoint - transform.position; targetVector.Normalize(); steerVector = Vector3.Slerp(steerVector, targetVector, turnSpeed);

 transform.LookAt(transform.position + steerVector);
 transform.Translate(steerVector * speed, Space.World);

 if (transform.position.y != ground) {
     RaycastHit hit;
     Vector3 origin = transform.position;
     origin.y += 1;
     if (Physics.Raycast(origin, -transform.up, out hit, 5f, groundLayer)) {         
         origin.y = hit.point.y;
         transform.position = origin;
     }
 }

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 Owen-Reynolds · Apr 02, 2011 at 07:04 AM 0
Share

World of Warcraft uses the "orient to normal" trick for non-bipeds, which gives them that excessively jerky tilt (pets near a wall flip on their sides.) Ins$$anonymous$$d, maybe check the ground near front and back and orient to that slope.

avatar image Patyrn · Apr 02, 2011 at 07:56 AM 0
Share

That requires two raycasts firing really often though. Aren't raycasts expensive? As long as I smooth the orientation it shouldn't be jerky.

avatar image StephanK · Apr 02, 2011 at 08:27 AM 0
Share

Raycasts are not that expensive. You can even limit them to be cast only against certain objects or layers to further optimize them.

2 Replies

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

Answer by StephanK · Apr 02, 2011 at 08:29 AM

Look at the Quaternion.LookRotation method. You can create a rotation that looks along a specific axis (steerVector) and has a specific up-direction (the ground 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 Owen-Reynolds · Apr 02, 2011 at 06:10 PM 0
Share

The ground normal is: TerrainData td = Terrain.activeTerrain.terrainData; Vector3 grndNorm = td.GetInterpolatedNormal(pos.x/td.size.x, pos.z/td.size.z); Can use that as 2nd input to Lookat (will give a small sideways tilt, but not extra pitch.)

avatar image Patyrn · Apr 02, 2011 at 10:13 PM 0
Share

$$anonymous$$y terrain is actually just a mesh not a unity Terrain.

avatar image
0

Answer by Owen-Reynolds · Apr 03, 2011 at 10:26 PM

This worked to angle something exactly with the ground. Disclaimers: I'm assuming you have some Y-rotation 0-360 variable (called mine facing.) It looks OK if the ground mesh is big compared to the thing being moved -- otherwise it will jerk around on the bumps. It's tested, but may have been copied incorrectly, and I have not tested the hit.normal part. It is in C#:

// after raycast, or however you get normal:
// Compute angle to tilt with ground:
Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
// Base angle is straight up, spun only on y:
// "facing" is the script variable used for turning:
transform.rotation = Quaternion.Euler(0,facing,0);
// tilt to align with ground:   
transform.rotation = grndTilt*transform.rotation;   

If makes kind of a cool snap when you land after a small jump. If you want smooth, replace transform.rotation with Quaternion wantRotation; (and blend with trans.rot however.)

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

No one has followed this question yet.

Related Questions

Quaternion amount/value of rotation? 2 Answers

Reverse of this function? 0 Answers

Setting up point sets for a procedural tree 1 Answer

Order matters when multiplying Quaternion by Vector3... Possible bug?? 1 Answer

Unity Rotate Raycast on Quaternion 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