Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotating Object using motortorque assistance needed

Discussion in 'Scripting' started by RaptorRaptures, Jul 1, 2014.

  1. RaptorRaptures

    RaptorRaptures

    Joined:
    Mar 5, 2014
    Posts:
    9
    Hi,

    The code you can see below, but basically with KeyCode.E and KeyCode.W work as they should where as KeyCode.S and KeyCode.D dont work at all? Whats supposed to happen is that when E is pressed the the front right tyre must move and the Front left tyre must not move(Creates a rotation effect on the object we using) and vice versa if W is pressed. if both are pressed then essentially the Object moves straight forward. Now with S and D this is supposed to do what E and W does but in a reverse way. But for some reason S and D cause the machine to move straight backwards whether they are both pressed or either one of them are pressed?

    var wheelFL : WheelCollider;
    var wheelFR : WheelCollider;
    var wheelRL : WheelCollider;
    var wheelRR : WheelCollider;

    function fixedUpdate()
    {

    if(currentSpeedL < topSpeed && currentSpeedR < topSpeed && currentSpeedL > -maxReverseSpeed && currentSpeedR > -maxReverseSpeed)
    {


    if(Input.GetKey(KeyCode.D))
    wheelFR.motorTorque = maxTorque * 1;

    if(Input.GetKey(KeyCode.S))
    wheelFL.motorTorque = maxTorque * 1;
    }
    else
    {
    wheelFL.motorTorque = -5;
    wheelFR.motorTorque = -5;
    wheelFL.brakeTorque = 15;
    wheelFR.brakeTorque = 15;
    }

    if(currentSpeedL < topSpeed && currentSpeedR < topSpeed && currentSpeedL > -maxReverseSpeed && currentSpeedR > -maxReverseSpeed)
    {

    if(Input.GetKey(KeyCode.E))
    wheelFR.motorTorque = maxTorque * -1;


    if(Input.GetKey(KeyCode.W))
    wheelFL.motorTorque = maxTorque * -1;

    }
    else
    {
    wheelFL.motorTorque = 5;
    wheelFR.motorTorque = 5;
    wheelFL.brakeTorque = 15;
    wheelFR.brakeTorque = 15;
    }
    }
     
  2. Wessels

    Wessels

    Joined:
    Apr 18, 2014
    Posts:
    6
    B, RPM :)
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    it seems to me like a logic error. You only set the motor torques back if either the W or S key is pressed. There needs to be logic as to what the user is trying to do with whatever keys down.

    (this may or may not be close)
    W turn right slowly
    E turn left slowly
    S turn left slowly
    D turn right slowly
    WE move forward
    SD move backward
    WD turn right quickly
    SE turn left quickly

    only some code I did in a text editor...
    Code (csharp):
    1.  
    2. function Update(){
    3.     var leftGas = 0;
    4.     var rightGas = 0;
    5.     var leftBrake = 15;
    6.     var rightBrake = 15;
    7.    
    8.     if(Input.GetKey(KeyCode.W)) leftGas = maxTorque;
    9.     if(Input.GetKey(KeyCode.E)) rightGas = maxTorque;
    10.     if(Input.GetKey(KeyCode.S)) leftGas = -maxTorque;
    11.     if(Input.GetKey(KeyCode.D)) leftGas = -maxTorque;
    12.    
    13.     if(leftGas != 0) leftBrake = 0;
    14.     if(rightGas != 0) rightBrake = 0;
    15.    
    16.     wheelFL.motorTorque = leftGas;
    17.     wheelFR.motorTorque = rightGas;
    18.     wheelFL.brakeTorque = leftBrake;
    19.     wheelFR.brakeTorque = rightBrake;
    20. }
    21.