Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

[Beta 21] Changing HingeJoint.motor has no effect

Discussion in 'Unity 5 Pre-order Beta' started by Lunatix, Feb 2, 2015.

  1. Lunatix

    Lunatix

    Joined:
    Dec 28, 2012
    Posts:
    11
    I experimented with a box and 4 connected cylinders, all connected to it with a hinge joint.
    When I change the motor from the editor, the vehicle starts moving - however, changing it dependent on keys at runtime via C# script has no effect.

    Complete project: https://www.dropbox.com/s/ov1s2h7a38mjkvf/Physics-Sandbox.7z?dl=0

    Code (CSharp):
    1. public class WheelMotor : MonoBehaviour {
    2.  
    3.     public KeyCode ForwardKey = KeyCode.UpArrow;
    4.     public KeyCode BackwardKey = KeyCode.DownArrow;
    5.     public float Force = 25.0f;
    6.     public float TargetVelocity = 150;
    7.  
    8.     private HingeJoint hingeJoint;
    9.  
    10.     // Use this for initialization
    11.     public void Start() {
    12.         hingeJoint = GetComponent<HingeJoint>();
    13.         hingeJoint.useMotor = true;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     public void FixedUpdate () {
    18.         if (hingeJoint != null) {
    19.             var motor = hingeJoint.motor;
    20.             var apply = false;
    21.  
    22.             if (Input.GetKeyUp(ForwardKey)) {
    23.                 motor.targetVelocity = 0;
    24.                 apply = true;
    25.             } else if (Input.GetKeyUp(BackwardKey)) {
    26.                 motor.targetVelocity = 0;
    27.                 apply = true;
    28.             }
    29.  
    30.             if (Input.GetKeyDown(ForwardKey)) {
    31.                 motor.targetVelocity = TargetVelocity;
    32.                 apply = true;
    33.             } else if (Input.GetKeyDown(BackwardKey)) {
    34.                 motor.targetVelocity = -TargetVelocity;
    35.                 apply = true;
    36.             }
    37.  
    38.             if (apply) {
    39.                 hingeJoint.motor = motor;
    40.             }
    41.            
    42.         }
    43.     }
    44. }
    45.  
     
    zangad likes this.