- Home /
Move car while colliding with "ground"
Hi guys
I'm trying to make my car drive while it touches the ground. I've got my car to move around without the colliding code, but it acts kind of like an aeroplane when it jumps in to the air. So I thought I'd make the car only gain speed while touching the ground.
Here's the code I'm trying to make it happen.
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "ground"){
print("hellllo");
if(Input.GetKey ("up")) {
transform.Translate(Vector3.forward * -Time.deltaTime*20);
}
if(Input.GetKey ("down")) {
print("hello");
transform.Translate(Vector3.forward * Time.deltaTime*6);
}
if(Input.GetKey("left")){
transform.Rotate(Vector3.up * Time.deltaTime*-90);
}
if(Input.GetKey("right")){
transform.Rotate(Vector3.up * Time.deltaTime*90);
}
}
}
It does go past the first two if statements, but doesn't do anything when I hit the arrow buttons.
Any help would be greatly appreciated.
I just realize I should use "OnCollisionStay", but it still doesn't work.
Answer by Chimera3D · May 04, 2012 at 01:35 AM
Hm... a better way to simulate this is to do it with physics. It's easier and more realistic. You can add rigidbodies to the car and the wheels. The wheels should use the wheel collider. Look up rididbody.AddTorque and use that in order to propel the wheels in their respective directions. Anyway if you want to go this route, then here's a fix.
First of all your code will not work without a rigidbody, otherwise the car will just float and it will do the "aeroplane" move. Second of all you are only calling that code when the car first touches the ground. Try something like this instead:
var isGrounded : boolean;
function OnCollisionEnter(){
if (collision.gameObject.tag == "ground"){
isGrounded = true;
}
}
function OnCollisionExit(){
if (collision.gameObject.tag == "ground"){
isGrounded = false;
}
}
Then say:
function Update(){
if(isGrounded){
if(Input.GetKey ("up")) {
transform.Translate(Vector3.forward * -Time.deltaTime*20);
}
if(Input.GetKey ("down")) {
print("hello");
transform.Translate(Vector3.forward * Time.deltaTime*6);
}
if(Input.GetKey("left")){
transform.Rotate(Vector3.up * Time.deltaTime*-90);
}
if(Input.GetKey("right")){
transform.Rotate(Vector3.up * Time.deltaTime*90);
}
}
}
That should work, I'm not able to actually test it though.
Hi Noob-E
That worked flawlessly. Thanks!
You're right it would probably be better to use addTorque on the wheels, but I'm just getting familiar with using Unity. Using
"rigidbody.velocity = transform.TransformDirection (Vector3.forward Time.deltaTime 300);"
works okay as well apart from the car can drive upside down at the moment :)
Answer by Skeletor · May 04, 2012 at 09:57 AM
Hi Noob-E
That worked flawlessly. Thanks!
You're right it would probably be better to use addTorque on the wheels, but I'm just getting familiar with using Unity. Using
"rigidbody.velocity = transform.TransformDirection (Vector3.forward Time.deltaTime 300);"
works okay as well apart from the car can drive upside down at the moment :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613071051im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I do Flexible curved car or car on curved force in Unity 5? 0 Answers
Traffic Script 1 Answer
Car Model wheels problem 0 Answers
collision not working at high speeds 3 Answers