//uncomment for original movement code void FixedUpdate() { if (isAI) { PathPlanningKart k = GetComponent <PathPlanningKart>(); k.PathPlanNextSegment(); Tuple <float, float> t = carController.speedAndTurn(this.gameObject); turnInput = (float)t.Second; forwardInput = (float)t.First; ItemsAI.updateItems(); k.UseItem(); } else if (Input.GetKeyUp(KeyCode.P)) { Time.timeScale = paused ? 1 : 0; paused = !paused; } else { turnInput = input.getTurnInput(); forwardInput = input.getForwardInput(); } //how quickly do we want to turn currentDeltaTurn = DELTA_TURN * turnInput; //takes care of ideling to go back straight && also turning //Debug.Log ("(turnValue + currentDeltaTurn): "+(turnValue + currentDeltaTurn)+" MAXTU: "+(MAX_TURN)+" speed: "+speed); if (Mathf.Abs(turnInput) < .1 && turnValue > 0) { turnValue = 0; // Debug.Log("Idle Pos"); if (turnValue < DELTA_TURN) { turnValue = 0; } else { turnValue -= DELTA_TURN; } } else if (Mathf.Abs(turnInput) < .1 && turnValue < 0) { turnValue = 0; // Debug.Log("Idle Neg"); if (turnValue > -DELTA_TURN) { turnValue = 0; } else { turnValue += DELTA_TURN; } } else if ((turnValue + currentDeltaTurn) < (MAX_TURN) && (turnValue + currentDeltaTurn) > (-MAX_TURN)) { // Debug.Log("Turn"); turnValue += currentDeltaTurn; } //Debug.Log("DeltaTurn: " + currentDeltaTurn); //Debug.Log("turnValue: " + turnValue); //Debug.Log("Vert: "+Input.GetAxis(vertical)); //which way do we want to go //takes care of ACCELeration speed += ACCEL * forwardInput; if (speed < -MAX_SPEED) { speed = -MAX_SPEED; } if (speed > MAX_SPEED) { speed = MAX_SPEED; } if (forwardInput == 1 && speed < MAX_SPEED) { speed += ACCEL; } else if (forwardInput == -1 && speed > -MAX_SPEED) { speed -= ACCEL; } else if (forwardInput == 0 && speed < 0) { if (speed > -IDLE_ACCEL) { speed = 0; } else { speed += IDLE_ACCEL; } } else if (forwardInput == 0 && speed > 0) { if (speed < IDLE_ACCEL) { speed = 0; } else { speed -= IDLE_ACCEL; } } else if (Mathf.Abs(speed) > MAX_SPEED) { speed = Mathf.Sign(speed) * MAX_SPEED; } //applies boost, if any speed = speed * boost; //calculates the direction displacement vector rotationVector.Set(0, turnValue * speed * Time.deltaTime, 0); direction = Quaternion.Euler(rotationVector) * direction; //Debug.Log ("Direction: "+ direction); //Debug.Log ("Speed: " +speed); transform.position += direction * (speed * Time.deltaTime); if (!goingBackwards) { transform.rotation = Quaternion.LookRotation(direction); } else { transform.rotation = Quaternion.LookRotation(-1 * direction); } //newEquations (); //equations(); //updateLogVectors (); if (writePositionsToFile) { WriteToRepo(); } //uncomment for original movement code }
// <summary> // Update is called once per frame // </summary> void FixedUpdate() { if (Time.realtimeSinceStartup - startTime > 4.5f) { print("counting down"); start = true; } if (start) { //Debug.Log("Delta Time: " + Time.deltaTime); Debug.Log("boost: " + boost); WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; float AntiRoll = 5000; bool groundedL = fl.GetGroundHit(out hit); if (groundedL) { travelL = (-fl.transform.InverseTransformPoint(hit.point).y - fl.radius) / fl.suspensionDistance; } bool groundedR = fr.GetGroundHit(out hit); if (groundedR) { travelR = (-fr.transform.InverseTransformPoint(hit.point).y - fr.radius) / fr.suspensionDistance; } var antiRollForce = (travelL - travelR) * AntiRoll; if (groundedL) { rb.AddForceAtPosition(fl.transform.up * -antiRollForce, fl.transform.position); } if (groundedR) { rb.AddForceAtPosition(fr.transform.up * antiRollForce, fr.transform.position); } bool bgroundedL = bl.GetGroundHit(out hit); if (bgroundedL) { travelL = (-bl.transform.InverseTransformPoint(hit.point).y - bl.radius) / bl.suspensionDistance; } bool bgroundedR = br.GetGroundHit(out hit); if (bgroundedR) { travelR = (-br.transform.InverseTransformPoint(hit.point).y - br.radius) / br.suspensionDistance; } if (bgroundedL) { rb.AddForceAtPosition(bl.transform.up * -antiRollForce, bl.transform.position); } if (bgroundedR) { rb.AddForceAtPosition(br.transform.up * antiRollForce, br.transform.position); } if (isAI) { //PathPlanningKart k = GetComponentInChildren<PathPlanningKart>(); kartp.PathPlanNextSegment(); Tuple <float, float> t = carController.speedAndTurn(this.gameObject); turnInput = (float)t.Second; forwardInput = (float)t.First; //Debug.Log("Forward Input:" + forwardInput + " Turn Input:" + turnInput); ItemsAI.updateItems(); kartp.UseItem(); } else { turnInput = input.getTurnInput(); forwardInput = input.getForwardInput(); //Debug.Log("Forward Input:" + forwardInput + " Turn Input:" + turnInput); } brakeInput = input.getBraking(); if (rb.velocity.magnitude < MAX_SPEED && forwardInput != 0) { //print("Y" + rb.velocity.magnitude + " X:" + forwardInput + " Z:" + boost); br.motorTorque = forwardInput * motorForce * boost; bl.motorTorque = forwardInput * motorForce * boost; } else { br.motorTorque = 0; bl.motorTorque = 0; } // } else { // br.brakeTorque = -1*forwardInput * brakeForce; // bl.brakeTorque = -1*forwardInput * brakeForce; // fr.brakeTorque = -1*forwardInput * brakeForce; // fl.brakeTorque = -1*forwardInput * brakeForce; // } // if (brakeInput > 0) { // br.motorTorque = 0; // bl.motorTorque = 0; // } fr.steerAngle = turnInput * turnForce; fl.steerAngle = turnInput * turnForce; br.brakeTorque = brakeInput * brakeForce * boost; bl.brakeTorque = brakeInput * brakeForce * boost; fr.brakeTorque = brakeInput * brakeForce * boost; fl.brakeTorque = brakeInput * brakeForce * boost; br.brakeTorque += studder * brakeForce; bl.brakeTorque += studder * brakeForce; fr.brakeTorque += studder * brakeForce; fl.brakeTorque += studder * brakeForce; if (writePositionsToFile) { WriteToRepo(); } Quaternion temp = transform.rotation; Vector3 tt = temp.eulerAngles; if (tt.x > 300 && tt.x < 357) { tt.x = 359.5f; } if (tt.x < 50 && tt.x > .5f) { tt.x = .5f; } if (tt.z > 300 && tt.z < 359.5f) { tt.z = 359.5f; } if (tt.z < 50 && tt.z > .5f) { tt.z = .5f; } tt.Set(tt.x, tt.y, tt.z); temp.eulerAngles = tt; transform.rotation = temp; } }