private void SetStopped() { currentSpeed = 0; currentGroundDirection = MoveGroundDirection.Stop; isAccelerating = false; isBraking = false; stickyDelayCount = 0; }
private void FixedUpdate() { if (gameManager.prefabLocation != null) { if (GameObject.Find(gameManager.prefabLocation.name).transform) { obj1 = GameObject.Find(gameManager.prefabLocation.name).transform; obj2 = transform; _distance = Vector3.Distance(obj1.position, obj2.position); } else { return; } } // Direction to move this update. Vector3 moveGroundDirection = Vector3.zero; // Direction requested this update. MoveGroundDirection requestedDirection = MoveGroundDirection.Stop; if (m_Controller.isGrounded) { // Simulate loss of turn rate at speed. float currentTurnRate = Mathf.Lerp(currentGroundDirection == MoveGroundDirection.Forward ? topForwardTurnRate : topReverseTurnRate, stoppedTurnRate, 1 - (currentSpeed / currentTopSpeed)); Vector3 angles = m_Transform.eulerAngles; angles.y += (Input.GetAxis("Horizontal") * currentTurnRate); m_Transform.eulerAngles = angles; // Based on input, determine requested action. if (Input.GetAxis("Vertical") > 0) // Requesting forward. { requestedDirection = MoveGroundDirection.Forward; isAccelerating = true; } else { if (Input.GetAxis("Vertical") < 0) // Requesting reverse. { requestedDirection = MoveGroundDirection.Reverse; isAccelerating = true; } else { requestedDirection = currentGroundDirection; isAccelerating = false; } } isBraking = false; if (currentGroundDirection == MoveGroundDirection.Stop) { stickyDelayCount = stickyDelayCount + Time.deltaTime; // If we are not sticky throttle or if we have hit the delay then change direction. if (!stickyThrottle || (stickyDelayCount > stickyThrottleDelay)) { // Make sure we can go in the requested direction. if (((requestedDirection == MoveGroundDirection.Reverse) && (topReverseSpeed > 0)) || ((requestedDirection == MoveGroundDirection.Forward) && (topForwardSpeed > 0))) { currentGroundDirection = requestedDirection; } } } else { // Requesting a change of direction, but not stopped so we are braking. if (currentGroundDirection != requestedDirection) { isBraking = true; isAccelerating = false; } } // Setup top speeds and move direction. if (currentGroundDirection == MoveGroundDirection.Forward) { moveGroundDirection = Vector3.forward; currentTopSpeed = topForwardSpeed; } else { if (currentGroundDirection == MoveGroundDirection.Reverse) { moveGroundDirection = -1 * Vector3.forward; currentTopSpeed = topReverseSpeed; } else { if (currentGroundDirection == MoveGroundDirection.Stop) { moveGroundDirection = Vector3.zero; } } } if (isAccelerating) { // If we haven't hit top speed yet, accelerate. if (currentSpeed < currentTopSpeed) { currentSpeed = currentSpeed + (acceleration * Time.deltaTime); } } else { // If we are not accelerating and still have some speed, decelerate. if (currentSpeed > 0) { // Adjust deceleration for braking and implement sticky throttle. float currentDecelerationRate = isBraking ? brakingDeceleration : (!stickyThrottle ? deceleration : 0); currentSpeed = currentSpeed - (currentDecelerationRate * Time.deltaTime); } } // If our speed is below zero, stop and initialize. if ((currentSpeed < 0) || ((currentSpeed == 0) && (currentGroundDirection != MoveGroundDirection.Stop))) { SetStopped(); } else { // Limit the speed to the current top speed. if (currentSpeed > currentTopSpeed) { currentSpeed = currentTopSpeed; } } moveGroundDirection = m_Transform.TransformDirection(moveGroundDirection); } // Implement gravity so we can stay grounded. moveGroundDirection.y = moveGroundDirection.y - (gravity * Time.deltaTime); moveGroundDirection.z = moveGroundDirection.z * (Time.deltaTime * currentSpeed); moveGroundDirection.x = moveGroundDirection.x * (Time.deltaTime * currentSpeed); m_Controller.Move(moveGroundDirection); }