- Home /
using Raycast.hit function my car gets up and doesnt gets down ? it is in flying state
hai everyone...iam new to unity...am trying to move a car in a terrain surface(mountain).using raycast.hit function i get my car up when am hitting in the mountain surface..but my car doesnt gets down...the position of the car cannot be rest to terrain i,e ground....here is my code...Thanks... car moving script..
public float movespeed=300;
public float turningSpeed=40;
start(){}
void update(){
float horizontal=Input.GetAxis("Horizontal")*turningSpeed*time.deltaTime;
transform.rotate(0,horizontal,0);
rigidbody.addforce(input.getaxis("Vertical")*movespeed*transform.right)
}
this is my raycast codings...
public class finddistance
{
public Transform target;
void start(){}
void FixedUpdate(){
RaycastHit hit;
if(physics.Raycast(transform.position,-vector3.down,out hit)){
float distanceGround=hit.distance;
float heightToAdd=transform.localScale.y;
target.Translate(0,transform.position.y-distanceGround+heightToAdd,0);
}
}
pls ignore the cases of the letters....and i dont have any colliders in my project.
Answer by Tricky7991 · Sep 11, 2014 at 05:04 PM
If I am understanding your question correctly. Basically you want your car to raise and lower in height depending on the height of the ground?
If so then all you would need to do is divide the distanceGround by 2.
target.Translate(0,distanceGround / 2,0);
And it should always bring you to the same height you where before. Unless you have multiple mountains in a row touching each other at different heights
What does it do? And give some more info. Like height of mountains, and the values of the transform.position.y
sorry cannot findout the height of the mountains...dont know how to find out......and transform.position.y value changes every time......