Search Unity

Controlling Hinge Direction to Drive

Discussion in 'Scripting' started by Shananno, Mar 26, 2015.

  1. Shananno

    Shananno

    Joined:
    Mar 25, 2015
    Posts:
    1
    Hi!

    I'm trying to create a cab and trailer that have their direction controlled by the angle of the joint/hinge (hitch)...like an articulating tractor. Has anyone done this before? I have the following script written, but my model in Unity doesn't react as expected. The log shows increases/decreases in Y value when the left/right arrow keys are pressed, and it moves forward/backward when the up/down arrows are pressed, but it won't turn on-screen. If you back up far enough, it eventually jackknifes (usually to the right, but sometimes to the left), but that's not exactly what I'm looking for. Has anyone had this problem? Any idea of a solution?

    Thanks for any input you might have!


    #pragmastrict
    var speed:float=5; //speed of mobile
    var turnSpeed: float=10; //turnspeed

    functionStart () {
    var trailerHinge = GetComponent(ConfigurableJoint);
    trailerHinge.targetRotation = Quaternion.Euler(0,0,0);
    }

    functionUpdate () {
    //grabtheinputaxis
    var ourCurrentRotation: Vector3;
    var steer=Input.GetAxis("Horizontal");
    var gas=Input.GetAxis("Vertical");
    var trailerHinge = GetComponent(ConfigurableJoint);
    var turnAngle: Vector3;

    var moveDist=gas*speed*Time.deltaTime;
    var howMuchHasAngleChangedSinceWeLastChecked=steer*turnSpeed*Time.deltaTime;

    ourCurrentRotation = trailerHinge.targetRotation.eulerAngles;

    turnAngle = ourCurrentRotation + Vector3(0, howMuchHasAngleChangedSinceWeLastChecked,0);
    trailerHinge.targetRotation = Quaternion.Euler(turnAngle);

    //Quaternion.Euler(0,howMuchHasAngleChangedSinceWeLastChecked,0) + trailerHinge.targetRotation;

    Debug.Log(trailerHinge.targetRotation.eulerAngles);

    //andnowmoveforwardbymoveVect
    transform.Translate(Vector3.forward*moveDist);
    }
     
    Last edited: Mar 26, 2015