# Vehicle keep on floor?

 0 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! more ▼ asked Mar 26 '12 at 06:36 PM kevinseligmann 116 ● 11 ● 16 ● 17 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

1 answer: sort voted first
 0 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.htmlLink to original question/answer (in C#) : http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html more ▼ answered Mar 26 '12 at 07:03 PM alucardj 13.8k ● 34 ● 56 ● 87 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! Mar 26 '12 at 09:47 PM kevinseligmann I have placed the script above for easy reference. It is worth reading teh links to get an understanding of what it does though. Mar 28 '12 at 10:46 AM alucardj 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 TickIf 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 =] Apr 11 '12 at 04:38 AM alucardj add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x1472
x122
x101
x39
x4

asked: Mar 26 '12 at 06:36 PM

Seen: 639 times

Last Updated: Apr 11 '12 at 04:38 AM