private void OnTriggerEnter(Collider other) { //We've hit a rail object if (other.transform.root.tag == "Rail") { if (other.transform.root.GetComponent <RailLogic>() != currentRail) { currentRail = other.transform.root.GetComponent <RailLogic>(); currentRail.SetReferenceRail(other.transform); currentRail.SetOnRail(true); currentRail.SetPlayerRailSpeed(currentSpeed); SetRailStatus(true); } } }
// Update is called once per frame void Update() { currentSpeed = velocity.magnitude; if (isGrounded) { isGrounded = Physics.CheckBox(transform.position + groundCheckOffset, groundCheckSize * .5f, Quaternion.identity, groundLayerMask); } else { isGrounded = Physics.CheckBox(transform.position + groundCheckOffsetAir, groundCheckSizeAir * .5f, Quaternion.identity, groundLayerMask); } Collider[] rails; if (isGrounded) { rails = Physics.OverlapSphere(transform.position + railGroundOffset, railGroundSize); } else { rails = Physics.OverlapSphere(transform.position + railAirOffset, railAirSize); } if (railCooldownTimer > 0) { railCooldownTimer -= Time.deltaTime; } if (rails.Length > 0) { float distance = 0; Collider nearest = rails[0]; foreach (Collider c in rails) { //Rail logic moved from OnTriggerEnter if (c.transform.root.tag == "Rail") { if (c.transform.root.GetComponent <RailLogic>() != currentRail) { if (railCooldownTimer < 0) { currentRail = c.transform.root.GetComponent <RailLogic>(); currentRail.SetReferenceRail(c.transform); currentRail.SetOnRail(true); currentRail.SetPlayerRailSpeed(currentSpeed); railCooldownTimer = railCooldown; SetRailStatus(true); } } } float dist = Vector3.SqrMagnitude(c.ClosestPoint(transform.position)); if (dist < distance) { distance = dist; nearest = c; } } } psm.HandleInput(); psm.UpdateState(); currentStateText.text = psm.currentState.ToString(); DebugDraw(); }