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!