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 /
avatar image
2
Question by JDubs212 · Mar 15, 2010 at 05:19 PM · wheelcollider

using wheel colliders?

I'm trying to use a wheel collider in a school project and was wondering A)how to script it to work with Input.GetKey() with the up arrow increasing the motorTorque.B)will it react with the terrain object moving the bike forward??

Any answers would be appreciated.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Jaap Kreijkamp · Mar 16, 2010 at 01:08 AM

Wheel colliders aren't easy things to control, when making a bike or motor you'll have to do some tricks to make it stable (not falling) as the wheel collider is infinitely thin. Below I wrote a simple motor script (that doesn't solve the falling over problem, but set y of centerOfMass property to negative values does help).

Because the wheelcolliders shouldn't rotate with the wheels, you need to set up the motor as follows for the script, the motor has two wheel attach gameobjects (frontwheelattach and backwheelattach), you must add the wheelcolliders to these nodes (and attach these to the script properties front/backWheelCollider). The attach nodes must have the actual wheels attached, and these should be connected to the script front/backWheelTransform.

If you need a demo project to see how to setup the script I could make something quick for that as well, just let me know.

// Jaap Kreijkamp // Ctrl-J (http://ctrl-j.com.au/) // Demo motor script, do with it whatever you like...

var frontWheelCollider : WheelCollider; var backWheelCollider : WheelCollider;

var frontWheelTransform : Transform; var backWheelTransform : Transform;

var motorPower : float = 50; var maxSteerAngle : float = 10; var centerOfMass : Vector3;

private var backWheelRotation : float = 0; private var frontWheelRotation : float = 0;

function Start() { rigidbody.centerOfMass = centerOfMass;
}

function UpdateWheelHeight(wheelTransform : Transform, collider : WheelCollider) { var localPosition : Vector3 = wheelTransform.localPosition; var hit : WheelHit;

 // see if we have contact with ground   
 if (collider.GetGroundHit(hit)) {
     // calculate the distance between current wheel position and hit point, correct
     // wheel position
     localPosition.y -= Vector3.Dot(wheelTransform.position - hit.point, transform.up) - collider.radius;
 }
 else {
     // no contact with ground, just extend wheel position with suspension distance
     localPosition = -Vector3.up * collider.suspensionDistance;
 }
 // actually update the position
 wheelTransform.localPosition = localPosition;

}

function Update() { var deltaTime : float = Time.deltaTime; var accel = Input.GetAxis("Vertical"); var steer = Input.GetAxis("Horizontal") * maxSteerAngle;

 // set power
 backWheelCollider.motorTorque = accel * motorPower;

 // set steering
 frontWheelCollider.steerAngle = steer;

 // calculate the rotation of the wheels
 frontWheelRotation = Mathf.Repeat(frontWheelRotation + deltaTime * frontWheelCollider.rpm * 360.0 / 60.0, 360.0);
 backWheelRotation = Mathf.Repeat(backWheelRotation + deltaTime * backWheelCollider.rpm * 360.0 / 60.0, 360.0);

 // set the rotation of the wheels
 frontWheelTransform.localRotation = Quaternion.Euler(frontWheelRotation, steer, 0.0);
 backWheelTransform.localRotation = Quaternion.Euler(backWheelRotation, 0, 0.0);

 // now some more difficult stuff: suspension, we have to manually move the wheels up and down
 // depending on the point of impact. As we have to do it twice (two wheels) we put it in a separate function
 UpdateWheelHeight(frontWheelTransform, frontWheelCollider);
 UpdateWheelHeight(backWheelTransform, backWheelCollider);

}

Comment
Add comment · Show 3 · 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 JDubs212 · Mar 22, 2010 at 03:50 PM 0
Share

thanks a lot, i put it in my project and assigned the wheel colliders for the front and back wheel but everytime i run it the wheels move to the center of the bike. Any suggestions?

avatar image JDubs212 · Mar 22, 2010 at 11:41 PM 0
Share

i've also got a problem with changing the motorPower using keyboard input

avatar image Kevin Ambruster · Sep 16, 2010 at 02:18 PM 0
Share

I had the same problem with the wheels moving to the center of my vehicle. First I found that when my vehicle came to a rest the collider.GetGroundHit function would return false and I also noticed that (localPosition = -Vector3.up * collider.suspensionDistance;) would set the local postion to (0,-suspensionDistance,0) which would be the pivot point of the parent object in most cases the center of the vehicle. Any body know why the GroundHit would return false just because the collider is at rest?

avatar image
2

Answer by taxvi · May 22, 2012 at 12:31 AM

I fixed the problem by myself (I'm so proud now :D) the reason of the wheels sticking in the middle of the vehicle is that in our UpdateeWheelHeight function there is this last line of code to be replaced: wheelTransform.localPosition = localPosition; with this: wheelTransform.localPosition.y = localPosition.y;

it's just that we have to control JUST and ONLY y axis of our wheels, not all 3 of them. so when the vehicle was in the air the code was repositioning the object on all of the axis - moving the whole wheel to (0,-suspensionDistance,0) in local coordinates.

PS: I found another minor mistake here: localPosition = -Vector3.up collider.suspensionDistance; it would be a better idea to subtract a radius of wheel from suspension distance: localPosition = -Vector3.up (collider.suspensionDistance - collider.radius);

Kevin: GetGroundHit is intended to produce false when the colider is in the air. that's why we are using the if statement :).

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
avatar image
0

Answer by UnityUnitedUK · May 01, 2014 at 03:19 PM

I know its a old post but could you make a demo project that works. thanks.

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 taxvi · May 02, 2014 at 06:40 AM 0
Share

oh, unfortunately my laptop on which I was building that project died long ago :/

this is the only part of the project that's still alive, but no source code, sorry: http://www.kongregate.com/games/taxvi/didgori

avatar image
0

Answer by malviyanpawan · Aug 13, 2014 at 03:17 PM

hi, I used ur code but there is little bit problem with it that when bike jump from any objects that point it look little bit buggy. So i replace with this code it work fine with my car demo project but look litter bit buggy with bike my code is :::

void WheelPosition() { Vector3 _wheelPos; RaycastHit _hit; if(Physics.Raycast(_rWheelCollider.transform.position, - _rWheelCollider.transform.up, out _hit, _rWheelCollider.radius + _rWheelCollider.suspensionDistance)) { _wheelPos = _hit.point + _rWheelCollider.transform.up _rWheelCollider.radius; } else { _wheelPos = _rWheelCollider.transform.position - _rWheelCollider.transform.up _rWheelCollider.suspensionDistance; } _wheelRTrans.position = _wheelPos; if(Physics.Raycast(_fWheelCollider.transform.position, - _fWheelCollider.transform.up, out _hit, _fWheelCollider.radius + _fWheelCollider.suspensionDistance)) { _wheelPos = _hit.point + _fWheelCollider.transform.up _fWheelCollider.radius; } else {
_wheelPos = _fWheelCollider.transform.position - _fWheelCollider.transform.up
_fWheelCollider.suspensionDistance; } _wheelFTrans.position = _wheelPos; }

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 Josh Naylor ♦♦ · Aug 13, 2014 at 03:18 PM 0
Share

Please format your code.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

applying a value to all items in an array[] 1 Answer

How to rotate at the same rpm wheels of a tank? 1 Answer

Benefit of nested classes? 1 Answer

How to import the object from server to unity 2 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