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 kevinseligmann · Mar 26, 2012 at 06:36 PM · terrainnormalsvehiclefloor

Vehicle keep on floor?

Hi people, I'm stuck at work with a project I'm developing. It consists of a car which has to move around pushing boxes to desired locations.

Because of the movement of the vehicle (It steers with the back wheels) and some other stuff, I decided not to make it a physics car.

So, I have a piece of code which moves the vehicle and steers it nice, but the problem is when my terrain is not flat. I've been reading around and found that you have to cast a ray, get the objects orientation and normals, and etc, but the trught is I'm not that good at maths and all my attempts to reproduce this, failed.

Could anyone give me a little hand here, enlightening me of how to do this? This is my actual code:


 private var speed : float = 2; // Speed of the car
 private var turnSpeed : float = 150; // Turn speed.
 
 var hit : RaycastHit;
 
 function Update()
 {
     var steer=Input.GetAxis("Horizontal");
     var gas=Input.GetAxis("Vertical");
     
     if (gas!=0)
     {
         var moveDist=gas*speed*Time.deltaTime;
         var turnAngle=steer * turnSpeed * Time.deltaTime * gas;
 
         transform.rotation.eulerAngles.y+=turnAngle;
         transform.Translate(Vector3.forward*moveDist);   
     }
     
     if(Physics.Raycast(transform.position, -Vector3.up, hit)) {
         Debug.Log(hit.distance + " -- " + hit.normal);
     }
 }


Thanks very much!

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
1
Best Answer

Answer by AlucardJay · Mar 26, 2012 at 07:03 PM

I recently tried something similar with a hoverboard. I had 4 raycasts to determine the ground rotation, but then I was advised to use the RaycastHit.normal.

I should point out both answers are by Aldo Naletto , not myself.

 private var curNormal: Vector3 = Vector3.up; // smoothed terrain normal
 private var iniRot: Quaternion; // initial rotation

 function Start(){
     iniRot = transform.rotation;
 }

 function Update(){
     var hit:RaycastHit;
     if (Physics.Raycast(transform.position, -curNormal, hit)){
         // curNormal smoothly follow the terrain normal:
         curNormal = Vector3.Lerp(curNormal, hit.normal, 4*Time.deltaTime);
         // calculate rotation to follow curNormal:
         var rot = Quaternion.FromToRotation(Vector3.up, curNormal);
         // combine with the initial rotation:
         transform.rotation = rot * iniRot;
         }
 }
 

Link to answer in my question (in JavaScript) : http://answers.unity3d.com/questions/230216/why-am-i-getting-a-flipped-out-effect-from-my-transform.rotate.html

Link to original question/answer (in C#) : http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html

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 kevinseligmann · Mar 26, 2012 at 09:47 PM 0
Share

Thanks very much for your reply @alucardj , I'll be checking the JS thread tomorrow at work to see if I can solve this issue. Thanks!

avatar image AlucardJay · Mar 28, 2012 at 10:46 AM 0
Share

I have placed the script above for easy reference. It is worth reading teh links to get an understanding of what it does though.

avatar image AlucardJay · Apr 11, 2012 at 04:38 AM 0
Share

Hello. Did this script work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.

I wish this was made clearer for new users.

How to accept an answer.

On the left-hand-side of the Answer box , there are the following icons :

Thumb Up Number (of votes) Thumb Down A Tick

If an answer worked for you , click on the 'Tick' , it should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , it should now be highlighted in green , and the number of votes should rise by 1. Following these simple steps helps the website work , and other readers to find answers also. Thanks =]

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

vehicle mesh gets trapped inside terrain mesh when collided 0 Answers

Increasing the size of terrain without destroying it. 1 Answer

Procedural mesh artifacts. 1 Answer

Vehicle collision with terrain 1 Answer

Correcting normals along chunk edges 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