- Home /
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.
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.
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?
make Sure you didn't attach the Front WheelJoint2D to both the front and back
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.
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.
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.
Please check this video https://www.youtube.com/watch?v=dH_UdByAWCU
Your answer
Follow this Question
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