Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 hafizhizers · Jan 27, 2017 at 12:50 AM · c#unity 5physicscarautomatic

How do I make car wheels spin and slow down when the car is stopped

Hye,how do I make car wheels spin and slow down when the car is stopped? the car is automatically move forward and player is allow to control left and right button.

Example car game like SkidStorm [https://play.google.com/store/apps/details?id=com.immobilegames.skidstorm&hl=en]

I am using the car script from the Samuel 3D tutorial,Help is very much appreciated. Thank you

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SimpleCarController : MonoBehaviour
 {
     //If you do not use center of mass the wheels base it off of the colliders
     //By using center of mass you can control where it is.
     public Transform t_CenterOfMass;
 
     //The maximum amount of power put out by each wheel.
     public float maxTorque = 500f;
 
     //The max distance a wheel can turn.
     public float maxSteerAngle = 45f;
 
     //Each wheel needs its own mesh
     public Transform[] wheelMesh = new Transform[4];
 
     //The physics of the wheels, max 20 axels.
     //WheelCollider[4] 4 is how many wheels we have.
     public WheelCollider[] wheelCollider = new WheelCollider[4];
 
     //Ridged body accessor.
     private Rigidbody r_Ridgedbody;
 
     public void Start()
     {
         // This sets where the center of mass is, if you look r_Ridgedbody."centerOfMass" is a function of ridged body.
         r_Ridgedbody = GetComponent<Rigidbody>();
         r_Ridgedbody.centerOfMass = t_CenterOfMass.localPosition;
     }
 
     public void Update()
     {
         //Sets the wheel meshs to match the rotation of the physics WheelCollider.
         UpdateMeshPosition();
     }
 
     public void FixedUpdate()
     {
         //Turn the wheels to a set max, with an input.
         float steer = Input.GetAxis("Horizontal") * maxSteerAngle;
         //Move forward or backwards based on the maxTorque, with an input.
         float torque = Input.GetAxis("Vertical") * maxTorque;
 
         //Sets which wheels turn, this is the two front wheels.
         wheelCollider[0].steerAngle = steer;
         wheelCollider[1].steerAngle = steer;
 
         //Sets which wheels move forward or backwards.
         for (int i = 0; i < 4; i++)
         {
             wheelCollider[i].motorTorque = torque;
         }
     }
 
     //Sets each wheel to move with the physics WheelColliders.
     public void UpdateMeshPosition()
     {
         for (int i = 0; i < 4; i++)
         {
             Quaternion quat;
             Vector3 pos;
 
             //Gets the current position of the physics WheelColliders.
             wheelCollider[i].GetWorldPose(out pos, out quat);
 
             ///Sets the mesh to match the position and rotation of the physics WheelColliders.
             wheelMesh[i].position = pos;
             wheelMesh[i].rotation = quat;
         }
     }
 }

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

Similar movement to a game (look description) 1 Answer

Car won't return to respawn point after hitting a trigger 2 Answers

Distribute terrain in zones 3 Answers

Unity 2D Car Side Scroller question. 1 Answer

Car trembling when reaching a certain speed 1 Answer


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