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
0
Question by ucefemad · Jul 11, 2017 at 10:03 PM · 2dunity 2dcarswheelsmotor

How to access two WheelJoint2D from C#?

 public WheelJoint2D FW;
 public WheelJoint2D BW;
 JointMotor2D frontMotor;
 JointMotor2D backMotor;
 
     public float acc = 100.0f;
 
 
     // Use this for initialization
     void Start () {
       
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetAxis("Vertical") > 0)
         {
             frontMotor.motorSpeed = acc * -1;
             frontMotor.maxMotorTorque = 10000;
             FW.motor = frontMotor;
             FW.useMotor = true;
 
             backMotor.motorSpeed = acc * -1;
             backMotor.maxMotorTorque = 10000;
             BW.motor = backMotor;
             BW.useMotor = true;
         }
 
         if (Input.GetAxis("Vertical") < 0)
         {
             frontMotor.motorSpeed = acc;
             frontMotor.maxMotorTorque = 10000;
             FW.motor = frontMotor;
             FW.useMotor = true;
 
             backMotor.motorSpeed = acc;
             backMotor.maxMotorTorque = 10000;
             BW.motor = backMotor;
             BW.useMotor = true;
         }
     }

I have a car body connected to two wheelJoint2D, When I Try to rotate the wheels, this code only rotates the front, it seems like it accesses only the front wheel, it doesn't see the back wheel? What am I doing wrong? Also, why can't the car be just front or rear wheel drive only? I doesn't move unless both wheel spin together at the same speed.

Comment
Add comment · Show 8
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 Cynikal ♦ · Jul 11, 2017 at 11:36 PM 0
Share

Are you throwing any errors at all? Have you tried attaching a breakpoint to the back$$anonymous$$otor section to see if it's getting triggered? (it should be). Do you maybe have a script on the front wheels that applys a rotation animation, but not on the back?

Also, what do you mean by why can't the cars be just front or rear wheel drive? They can be. It's your game. Code it the way you want to.

avatar image ucefemad Cynikal ♦ · Jul 12, 2017 at 08:17 AM 0
Share

No, it doesn't give me any error, in the WheelJoint2D properties, it shows that the first WheelJoint2D only is used, whatever it is for the front or back wheel, it's just the first WheelJoint2D, and it gives it the speed properly and everything works fine for the first wheelJoint2D, but the second WheelJoint2D isn't working at all, it's like the code can't modify any of its attributes.

So just to make sure I'm on the right path, I have the car body which is a rigidbody2d, with a polygon collider, and two WheelJoint2D, one for the front wheel, and one for the back wheel, and i attached the script to the car body. What could possibly be wrong?

avatar image Cornelis-de-Jager · Jul 12, 2017 at 01:33 AM 0
Share

make Sure you didn't attach the Front WheelJoint2D to both the front and back

avatar image ucefemad Cornelis-de-Jager · Jul 12, 2017 at 07:19 AM 0
Share

I'm sorry, I'm not sure what you mean, the car body has two WheelJoint2D, one for the front and one for the back, the WheelJoint2D are attached to the car body not each other.

avatar image EllisonLee · Jul 12, 2017 at 09:50 AM 0
Share

Can you give a screenshot about the inspector of the wheel? I create a test scene just as you said and It works well. $$anonymous$$ore information will be helpful for finding the reason.

avatar image ucefemad EllisonLee · Jul 12, 2017 at 02:23 PM 0
Share

That's the inspector

alt text

unity.png (114.5 kB)
avatar image EllisonLee ucefemad · Jul 13, 2017 at 01:11 AM 0
Share

So you put two Wheel Joint 2D on the same gameobject(CarBody) right?

Both of the parameter FW and BW will find the first Wheel Joint 2D attach on the CarBody because it at the top.(which connect to the FW Rigidbody2D)

If you change the two WheelJoint2D order. I think the BW will work and FW will not.

I think you should set the WheelJoint2D to each of the wheel like this. alt text

Please check this video https://www.youtube.com/watch?v=dH_UdByAWCU

Show more comments

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

125 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 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

Multiple Cars not working 1 Answer

Player Movement doesn't work, but Debug.Log shows that it should 1 Answer

How do I manage multiple colliders on a gameObject? 1 Answer

2D URP: Determine amount of light hitting object or specific point in scene 0 Answers

URP lighting 2d not working on Android,URP 2D lighting not working on Android 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