• 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 Grady · Apr 23, 2011 at 10:36 AM · car

car script error

Hey everyone,

I have been following a tutorial at http://carpe.com.au/slawia/2009/08/unity-wheel-collider-introduction I have managed to set up the wheel colliders and i can get the car to drive, except the back wheel won't spin with the car model that i am using, and the front wheels, instead of spinning around in a forward direction like normal cars, they spin inwards so from the side of they car inwards. To explain it a little bit easier, the z axis is what the front of the car is facing to, and instead of spinning around the z axis, they are spinning around the x axis. I am not sure if it has anything to do with the fact that there are variables for 6 wheels, as the tutorial was meant for a truck that has 6 wheels, but i just left the middle ones out. Below is the script for the car:

var rearWheel1 : WheelCollider; var rearWheel2 : WheelCollider; var frontWheel1 : WheelCollider; var frontWheel2 : WheelCollider;

var wheelFL : Transform; var wheelFR : Transform; var wheelML : Transform; var wheelMR : Transform; var wheelRL : Transform; var wheelRR : Transform;

var steer_max = 20; var motor_max = 40; var brake_max = 100; var steerSpeed = 20;

private var steer = 0; private var forward = 0; private var back = 0; private var brakeRelease = false; private var motor = 0; private var brake = 0; private var reverse = false; private var speed = 0;

function Start() { rigidbody.centerOfMass = Vector3(0, -0.05, 0); }

function FixedUpdate () {

speed = rigidbody.velocity.sqrMagnitude; steer = Input.GetAxis("Horizontal"); forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1); back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);

if(speed == 0 && forward == 0 && back == 0) { brakeRelease = true; }

if(speed == 0 && brakeRelease) { if(back > 0) { reverse = true; } if(forward > 0) { reverse = false; } }

if(reverse) { motor = -1 * back; brake = forward; } else { motor = forward; brake = back; } if (brake > 0 ) { brakeRelease = false; };

rearWheel1.motorTorque = motor_max motor; rearWheel2.motorTorque = motor_max motor; rearWheel1.brakeTorque = brake_max brake; rearWheel2.brakeTorque = brake_max brake;

if ( steer == 0 && frontWheel1.steerAngle != 0) { if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed Time.deltaTime)) { frontWheel1.steerAngle = 0; } else if (frontWheel1.steerAngle > 0) { frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed Time.deltaTime); } else { frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed Time.deltaTime); } } else { frontWheel1.steerAngle = frontWheel1.steerAngle + (steer steerSpeed Time.deltaTime); if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; } if (frontWheel1.steerAngle < -1 steer_max) { frontWheel1.steerAngle = -1 * steer_max; } } frontWheel2.steerAngle = frontWheel1.steerAngle; wheelFL.localEulerAngles.y = frontWheel1.steerAngle; wheelFR.localEulerAngles.y = frontWheel2.steerAngle;

wheelFR.Rotate(0, 0, frontWheel1.rpm -6 Time.deltaTime); wheelFL.Rotate(0, 0, frontWheel2.rpm -6 Time.deltaTime); wheelMR.Rotate(0, 0, rearWheel1.rpm -6 Time.deltaTime); wheelML.Rotate(0, 0, rearWheel2.rpm -6 Time.deltaTime); wheelRR.Rotate(0, 0, rearWheel1.rpm -6 Time.deltaTime); wheelRL.Rotate(0, 0, rearWheel2.rpm -6 Time.deltaTime);

}

[EDIT] i have realised that if i put the rear wheels in the middle wheels slots, they will turn now, but in the wrong way just like the front wheels[EDIT]

I hope somebody can help me out. If you want to see the demo on how it should actually work, you can go to the actual website where the script is located at http://carpe.com.au/slawia/2009/08/unity-wheel-collider-redux/

I hope somebody can help me... Thanks

Grady

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Brake Light Button 1 Answer

Make a noise when i crash into something 1 Answer

need tutorials for rigidbody.Addforce 1 Answer

Make car CenterOfMass change dynamically 1 Answer

Why are my axles with rigidbodies falling off my car? 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