private void motoMove(MotoInput input) { WColForward.steerAngle = Mathf.Clamp(input.steer, -1, 1) * maxSteerAngle; WColForward.brakeTorque = maxForwardBrake * input.brakeForward; WColBack.brakeTorque = maxBackBrake * input.brakeBack; WColBack.motorTorque = maxMotorTorque * input.acceleration; }
private MotoInput motoControl(MotoInput input) { var posNow = thisTransform.position; var speed = (posNow - prevPos) / Time.fixedDeltaTime; prevPos = posNow; speedVal = speed.magnitude; var moveForward = speed.normalized; var angle = Vector3.Dot(moveForward, Vector3.Cross(thisTransform.up, new Vector3(0, 1, 0))); var omega = (angle - prevAngle) / Time.fixedDeltaTime; prevAngle = angle; prevOmega = omega; float lowSpeed = 8f; float highSpeed = 25f; if (speedVal < lowSpeed) { float t = speedVal / lowSpeed; input.steer *= t * t; omega *= t * t; angle = angle * (2 - t); input.acceleration += Mathf.Abs(angle) * 3 * (1 - t); } if (speedVal > highSpeed) { float t = speedVal / highSpeed; if (omega * angle < 0f) { omega *= t; } } input.steer *= (1 - 2.5f * angle * angle); input.steer = 1f / (speed.sqrMagnitude + 1f) * (input.steer * steerSensivity + angle * controlAngle + omega * controlOmega); float steerDelta = 10 * Time.fixedDeltaTime; input.steer = Mathf.Clamp(input.steer, prevSteer - steerDelta, prevSteer + steerDelta); prevSteer = input.steer; return(input); }
void FixedUpdate() { var input = new MotoInput(); if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { input.acceleration = 1; } if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { input.steer += 1; } if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { input.steer -= 1; } if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { input.brakeBack = 0.3f; input.brakeForward = 0.8f; } if (Input.GetKey(KeyCode.Space)) { input.brakeBack = 1f; } motoMove(motoControl(input)); updateWheels(); if (Input.GetKeyDown(KeyCode.Backspace)) { pickUp(); } if (speedVal < 0.01f && thisTransform.up.y < 0.2f && Mathf.Abs(prevOmega) < 0.001f) { pickUp(); } }