private bool CheckForInteractableMachine(out MachinePlayerInteraction interactableMachine) { Ray _ray = new Ray(this.transform.position - new Vector3(0f, -0.5f, 0f), this.transform.forward); RaycastHit[] _hits = Physics.RaycastAll(_ray, characterController.radius * 2f); RaycastHit _nearestMachineHit = new RaycastHit(); float _smallestDistance = characterController.radius * 2.1f; bool _hasHitMachine = false; foreach (RaycastHit hit in _hits) { if (hit.collider.tag == "Machine") { float _distance = Vector3.Distance(hit.point, this.transform.position); if (_distance < _smallestDistance) { _smallestDistance = _distance; _nearestMachineHit = hit; _hasHitMachine = true; } } } bool _hasHitInteractableMachine = false; interactableMachine = null; if (_hasHitMachine) { interactableMachine = _nearestMachineHit.collider.GetComponent <MachinePlayerInteraction> (); if (!interactableMachine.Equals(null)) { _hasHitInteractableMachine = true; } } return(_hasHitInteractableMachine); }
//###################################################################################################### // Update //###################################################################################################### // Update is called once per frame void Update() { //#################################################################################### //death if (!isDead && !isDashing) { if (IsAboveVoid()) { isDead = true; respawnTimer = 0f; isDashing = false; isBumped = false; movementAcceleration.Set(0, -PlayerConstants.FALL_ACCELERATION, 0); } } else if (isDead) { if (respawnTimer >= PlayerConstants.RESPAWN_COOLDOWN) { isDead = false; transform.position = spawnPosition; transform.forward = new Vector3(0, 0, 1); movementAcceleration.Set(0, 0, 0); velocity.Set(0, 0, 0); } else { respawnTimer += Time.deltaTime; } } //#################################################################################### //movement //Debug.Log ("PlayerController " + joystickIndex + ": Update: acceleration=" + movement_acceleration.ToString ()); Vector3 _totalAcceleration = new Vector3(); //normal movement if (!isDashing) { dashCooldown = Mathf.Clamp(dashCooldown - Time.deltaTime, 0, PlayerConstants.DASH_COOLDOWN); _totalAcceleration += movementAcceleration; //Debug.Log ("PlayerController " + joystickIndex + ": Update: movementVelocity=" + movement_velocity.ToString ()); } //dashing movement else if (isDashing) { if (dashTimer >= PlayerConstants.DASH_DURATION) { isDashing = false; dashCooldown = PlayerConstants.DASH_COOLDOWN; } else { dashTimer += Time.deltaTime; _totalAcceleration += dashAcceleration; //Debug.Log ("PlayerController " + joystickIndex + ": Update: dashVelocity=" + dash_velocity.ToString ()); } } if (isBumped) { if (bumpTimer >= PlayerConstants.BUMP_DURATION) { isBumped = false; } else { bumpTimer += Time.deltaTime; _totalAcceleration += bumpAcceleration; } } velocity += _totalAcceleration - PlayerConstants.MOVEMENT_FRICTION * velocity; velocity = Vector3.ClampMagnitude(velocity, PlayerConstants.MOVEMENT_MAX_VELOCITY); characterController.Move(velocity * Time.deltaTime); if (velocity.normalized.magnitude != 0 && !isDead) { //transform.rotation = Quaternion.LookRotation (velocity.normalized); transform.forward = velocity.normalized; } //#################################################################################### //mashine interaction bool _interruptInteraction = false; MachinePlayerInteraction _nearestInteractableMachine; bool _hasHitInteractableMachine = CheckForInteractableMachine(out _nearestInteractableMachine); if (isInteracting && (isDashing || isBumped)) { _interruptInteraction = true; } else if (_hasHitInteractableMachine && isInteracting && _nearestInteractableMachine != currentMachine) { _interruptInteraction = true; } else if (isInteracting && !_hasHitInteractableMachine) { _interruptInteraction = true; } else if (bButtonCurrentlyPressed != bButtonPreviouslyPressed) { if (isInteracting && bButtonPreviouslyPressed && !bButtonCurrentlyPressed) { _interruptInteraction = true; } else if (!isInteracting && _hasHitInteractableMachine && !bButtonPreviouslyPressed && bButtonCurrentlyPressed) { isInteracting = true; currentMachine = _nearestInteractableMachine; currentMachine.OnStartInteraction(this); } } if (_interruptInteraction) { isInteracting = false; currentMachine.OnEndInteraction(this); currentMachine = null; } //#################################################################################### OnUpdate(); }