Search Unity

Problem with wheeljoint2d motor: nothing happens when changing speed

Discussion in 'Physics' started by sandbaydev, Dec 21, 2014.

  1. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    For some reason I'm unable to adjust the "motor" value of a wheeljoint2D.

    Code (csharp):
    1.  
    2. public JointMotor2D motor;
    3.  
    4. //...
    5. void Start() {
    6. this.motor = this.gameObject.GetComponent<WheelJoint2D>().motor;
    7. }
    8.  
    9.     void Update() {
    10.         this.motor.motorSpeed = -1000f;  
    11.        Debug.Log(this.motor.motorSpeed); // this will print "-1000f" but nothing happens
    12.      
    13.     }
    14.  
    The above code prints "-1000f" just fine, but in the inspector the motor speed remains 0.

    If I adjust the motor speed in inspector, the wheel starts rotating just fine.

    Any idea what could be wrong? (Using unit y4.6.1f1)
     
  2. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    Okay, I got it sorted out. What a strange thing :) After "getting motor" I must adjust values and "set motor back to wheeljoint".

    So, here's examples:
    Code (csharp):
    1.  
    2. // this is the basic setup...
    3.     public WheelJoint2D wheeljointFront;
    4.     public JointMotor2D motorFront;
    5.  
    this one does NOT work:
    Code (csharp):
    1.  
    2. //this doesn't work
    3.         this.motorFront = this.wheeljointFront.motor;
    4.         this.motorFront.motorSpeed = -1000f;  
    5.  
    here the thing that works:
    Code (csharp):
    1.  
    2.         this.motorFront = this.wheeljointFront.motor;
    3.         this.motorFront.motorSpeed = -1000f;  
    4.         this.wheeljointFront.motor = this.motorFront;
    5.