I am new to using Unity so this question may have been answered (but I could not find it answered on the forums) but I have a car that goes off road. When going up/down hills the car stays parallel and cuts into the terrain. Not sure how to fix this problem =/
Hello!
how are you building your car?, which colliders you have?, how’s your code?
if you can give us more info we might be able to help you out
I just have a basic character controller added to my game object
private var jumpSpeed:float = 8.0;
private var gravity:float = 20.0;
private var runSpeed:float = 5.0;
private var walkSpeed:float = 1.0;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
function Update ()
{
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
if(Input.GetMouseButton(1) Input.GetAxis("Horizontal") Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
if(Input.GetMouseButton(2) || Input.GetMouseButton(1))
Screen.lockCursor = false;
else
Screen.lockCursor = false;
// Toggle walking/running with the T key
if(Input.GetKeyDown("t"))
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.Below) != 0;
}
@script RequireComponent(CharacterController)
Thats my character controller script. It is a robot that has 6 wheels and as of right now no colliders
Ok, so you have a character controller, and when you press “play” The character doesnt go trough the terrain?
have you checked that your “capsule Collider” of your character controller is really going trough your mesh? (checking the gizmos).
The “fall through terrain” happens also using wheel colliders and a vehicle, this has to be something more to do with poly size on the terrain vs poly size of the colliding object with the terrain poly’s. An object that has multiple colliders like a vehicle works fine on flat surfaces, but when adding a terrain object and multiple colliders, it has a habbit of falling through. I know the problem he is talking about, just don’t know a solution to it since I don’t know how to control the collision sensativity value in Unity. I’ll make a video of this issue and post a link.
The vehicle has a standard mesh collider, the wheels have wheel colliders and of course the terrain is what it is so basically a static mesh collider. So basically this is a poly->poly collision issue with the terrain.
And without further waiting, here is a video… lovely eh:
http://screencast.com/t/A32G6xPfLlG
On the video, how he is going up and down with the terrain, is what I was talking about
My object just stays parallel, not moving with the terrain. Pardon the bad modeling/lighting/quality.
He is on a slope but does not slant with the slope
Maybe I am not putting mesh colliders onto the correct objects
Separate out the wheels from the model so that when you import the model they are children to the main body of the model, then put a mesh collider on the body model and wheel colliders on all of the wheels, look at the networking tutorial, it has a racing game in it, see how the movement is setup and make your two front tires the “front”, the 2 back tires the “back” and the rest just have wheel colliders equal to the back. If you want you can have all 4 front assigned as front tires but you have to update the code to have the second front set to turn at 1/2 that of the main two front. Works for the transport type vehicles.