Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by IcaroSL · Jun 08, 2016 at 04:27 AM · scripting problemcolliderscript.carwall

Make the car stop by hitting the wall

Hi There! I'm trying to make a car game that when the car hits a wall it stops working.

The problem is that I only managed to make the upper parts of the car to stop, but the wheel colliders keep working and the car keeps going after hitting the wall. i've used the C# script:

 public class CarScript : MonoBehaviour
  {    
     public float maxTorque;
     public Transform centerOfMass;
     private bool isDead = false;
     public WheelCollider[] wheelColliders = new WheelCollider[4];
     public Transform[] tireMeshes = new Transform[4];
 
     private Rigidbody m_rigidBody;
 
     void Start()
     {
         m_rigidBody = GetComponent<Rigidbody>();
         m_rigidBody.centerOfMass = centerOfMass.localPosition;
     }
 
    void Update()
     {
         if (isDead)
             return;
 
         UpdateMeshesPosition();
     }
     
     void FixedUpdate()
     {
         float steer = Input.GetAxis ("Horizontal");
         float accelerate = Input.GetAxis ("Vertical");
 
         float finalAngle = steer * 41f;
         wheelColliders[1].steerAngle = finalAngle;
         wheelColliders[0].steerAngle = finalAngle;
 
         for (int i = 0; i < 4; i++)
         {
         
             wheelColliders[i].motorTorque = accelerate;
             wheelColliders[i].motorTorque = maxTorque;
         }
     }
 
     
     void OnTrigger (Collider other)
     {
         if (other.tag == "wall")
             Death ();
         
     }
     
     private void Death()
     {
         isDead = true;
     }
 



I'm new to Unity and as you can see, I do not have much KNOWLEGDE in programming and i think i did something wrong in this script, so... any help would be appreciated.

THANK YOU

*Sorry for my english, hope you can understand.

Comment
Add comment · Show 2
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 5c4r3cr0w · Jun 08, 2016 at 04:42 AM 0
Share

Also write this condition in FixedUpdate() function

 if (isDead)
              return;


avatar image IcaroSL 5c4r3cr0w · Jun 09, 2016 at 02:51 AM 0
Share

it does'nt work, the car keeps moving. thank you for answer

1 Reply

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

Answer by RayCastX · Jun 09, 2016 at 03:27 AM

     private void Death()
          {
              isDead = true;
             this.GetComponent<Rigidbody>().isKinematic = true;
         float steer = Input.GetAxis ("Horizontal");
          float accelerate = Input.GetAxis ("Vertical");
  
          float finalAngle = steer * 41f;
          wheelColliders[1].steerAngle = finalAngle;
          wheelColliders[0].steerAngle = finalAngle;
  
          for (int i = 0; i < 4; i++)
          {
          
              wheelColliders[i].brakeTorque = Mathf.Infinity;
          }
 
          }
 private void Respawn(){
   isDead =false;
     this.GetComponent<Rigidbody>().isKinematic = false;
   
 }


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 IcaroSL · Jun 09, 2016 at 05:02 AM 0
Share

Heey RayCastX Thanks for your answer! It worked for me. I had to make some changes 'cause the car wasnt moving, i don't know exactly why, so i did the following:

 void OnTriggerEnter (Collider other)
     {
         if (other.tag == "Wall")
             Death();
     }
 
     void FixedUpdate()
     {
     
 
         float steer = Input.GetAxis ("Horizontal");
         float accelerate = Input.GetAxis ("Vertical");
         
         float finalAngle = steer * 41f;
         wheelColliders[1].steerAngle = finalAngle;
         wheelColliders[0].steerAngle = finalAngle;
         
         for (int i = 0; i < 4; i++)
                     
         {
             wheelColliders[i].motorTorque = accelerate;
             wheelColliders[i].motorTorque = maxTorque;
 
             {
                 if (isDead)
                 wheelColliders[i].brakeTorque = $$anonymous$$athf.Infinity;
             }
         }
     }
 
     private void Death()
     {
         isDead = true;
         this.GetComponent<Rigidbody> ().is$$anonymous$$inematic = true;
 
     }
 
     private void Respawn()
     {
         isDead = false;
         this.GetComponent<Rigidbody> ().is$$anonymous$$inematic = false;
     }
 
 


Thanks a lot for the help!

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

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make brake car ? 2 Answers

How to teleport a car? 1 Answer

wheel collider / script offsets the meshes 0 Answers

Unity2d move enemy in opposite direction on collision 1 Answer

The laser problems 0 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