Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skeletor · May 04, 2012 at 12:15 AM · collisioncarcarsdrive

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Skeletor · May 03, 2012 at 11:31 PM 0
Share

I just realize I should use "OnCollisionStay", but it still doesn't work.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Skeletor · May 04, 2012 at 09:37 AM 0
Share

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 :)

avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges