public void Attack() { //if (!isServer) // return; Collider[] colliders = Physics.OverlapSphere(attackTransform.position, 3f); foreach (Collider overlappedCollider in colliders) { DefenderCharacter defender = overlappedCollider.GetComponent <DefenderCharacter>(); if (defender != null) { defender.TakeDamage(5); } } }
private void FixedUpdate() { if (!hasAuthority) { return; } //Generate an imaginary sphere at the character's feet and see if it collides with anything on the ground layer. //If it does, then we are on the ground. Otherwise we are not on the ground if (Physics.CheckSphere(transform.position, .1f, whatIsGround)) { grounded = true; } else { grounded = false; } //If the GameManager exists AND it tells us that the game is over, leave if (GameManager.instance != null && GameManager.instance.CurrentGameState == GameState.Gameover) { //Go back to our Idle animation by dropping the speed to 0 anim.SetFloat("Speed", 0f); return; } //Get the horizontal and vertical input (up/down/left/right arrows, WASD keys, controller analog stick, etc), //and store that input in our playerInput variable (there won't be any "y" input) playerInput.Set(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); //Adjust to camera space playerInput = Quaternion.Euler(Vector3.up * cameraTransform.rotation.eulerAngles.y) * playerInput; bool canRun = anim.GetCurrentAnimatorStateInfo(0).IsName("Idle") || anim.GetCurrentAnimatorStateInfo(0).IsName("Run") || anim.GetCurrentAnimatorStateInfo(0).IsName("Jump"); if (GameManager.instance.CurrentGameState != GameState.Playing) { canRun = false; } //Tell the animator the "speed" of our movement based on the magnitude //of the vector (the numerical "value" of the vector) if (canRun) { anim.SetFloat("Speed", playerInput.sqrMagnitude); } else { anim.SetFloat("Speed", 0f); } //Get Attack state if (Input.GetMouseButtonDown(0)) { anim.SetBool("Attack", true); RaycastHit hit; Ray ray = new Ray(transform.position + Vector3.up * 0.5f, transform.forward); Debug.DrawRay(ray.origin, ray.direction * 2f, Color.blue, 20f); if (Physics.Raycast(ray, out hit, 2f)) { DefenderCharacter tower = hit.collider.GetComponent <DefenderCharacter>(); if (tower != null) { tower.TakeDamage(5); } } CancelInvoke("AttackCooldown"); Invoke("AttackCooldown", 0.5f); } //If there is no input from the player, we're done here and can leave if (playerInput == Vector3.zero) { return; } //Use the "Quaternion" class to determine the rotation we need to face the direction we want to go Quaternion newRotation = Quaternion.LookRotation(playerInput); //If we need to turn and face a new direction, use the RotateTowards() method to turn quickly, but not //instantly (which looks better) if (rigidBody.rotation != newRotation) { rigidBody.rotation = Quaternion.RotateTowards(rigidBody.rotation, newRotation, turnSpeed * Time.deltaTime); } if (canRun) { //We take our input, multiply it by our speed, and then multiply Time.deltaTime. We then //add this amount to our current position to get the new desired position. NOTE: We "normalize" //our input so that the player won't move faster going diagonolly. NOTE: multiplying the value //with Time.deltaTime ensures that everyone has the same gameplay regardless of the speed of their //computers or the physics settings of the game Vector3 newPosition = transform.position + playerInput.normalized * movementSpeed * Time.deltaTime; //Use the rigidbody to move to the new position. This is better than Transform.Translate since it means //the player will mvoe with physics and force instead of just "teleporting" to the new spot rigidBody.MovePosition(newPosition); } }