public void Init(PlayerDefinition playerDefinition) { level = 1; experience = 0; health = playerDefinition.MaxHealth(level); levelupThreshold = playerDefinition.baseLevelupThreshold; }
void FixedUpdate() { if (Input.GetButtonDown("Fire1")) { mAttacking = true; } else { if (mAttacking) { var currentAnim = mAnimator.GetCurrentAnimatorStateInfo(0); if (currentAnim.IsName("Attack") && currentAnim.normalizedTime >= 1.0f) { mAttacking = false; } } } // Mouse look const float coeff = 5.0f; transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")) * coeff); if (mAttacking) { mRigidBody.velocity = new Vector3(); targetObjectForInteraction = null; } else { // Player movement PlayerDefinition playerDefinition = GameController.Instance.playerDefinition; float x = Input.GetAxis("Horizontal") * playerDefinition.strafeSpeed; float y = Input.GetAxis("Vertical") * playerDefinition.forwardSpeed; Vector3 direction = transform.forward * y + transform.right * x; mRigidBody.velocity = direction; // See if player can interact with something int n = Physics.OverlapSphereNonAlloc(transform.position, interactRadius, mColliderBuffer, -1, QueryTriggerInteraction.Ignore); AbstractInteractable nearestInteractable = null; float nearestDistance = 0.0f; for (int i = 0; i < n; i++) { GameObject gameObject = mColliderBuffer[i].gameObject; if (gameObject == this) { continue; } AbstractInteractable interactable = gameObject.GetComponent <AbstractInteractable>(); if (interactable != null && interactable.CanInteract()) { Vector3 dir = gameObject.transform.position - transform.position; dir.y = 0; float angle = Vector3.Angle(transform.forward, dir); if (Mathf.Abs(angle) > 45.0f) { continue; } float sqrDistance = dir.sqrMagnitude; if (nearestInteractable == null || sqrDistance < nearestDistance) { nearestInteractable = interactable; nearestDistance = sqrDistance; } } } targetObjectForInteraction = nearestInteractable; } // Use if (Input.GetButtonDown("Use") && targetObjectForInteraction != null) { targetObjectForInteraction.Interact(); } mSpeed = mRigidBody.velocity.magnitude; mAnimator.SetFloat("speed", mSpeed); mAnimator.SetBool("attack", mAttacking); }