void FixedUpdate() { _animator.SetSpeed(_animation_speed); if (_controllable) { if (clickToMove) { if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray); foreach (RaycastHit hit in hits) { bool done = false; foreach (string tag in clickableTags) { if (hit.transform.CompareTag(tag)) { _wanted_position = hit.point; _last_distance = Vector3.Distance(_t.position, _wanted_position); done = true; break; } } if (done) { break; } } } } } _animation_speed = 1; float input_modifier = (_input_x != 0.0f && _input_y != 0.0f) ? 0.7071f : 1.0f; if (_controllable) { if (keyboardControl) { _input_x = Input.GetAxis("Horizontal"); _input_y = Input.GetAxis("Vertical"); /* * Uncomment this code if you want to add a strafing axis * _input_s = Input.GetAxis("Strafe"); */ } } // If enabled, always rotate to look at the mouse position if (alwaysFaceMouse) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { _t.LookAt(new Vector3(hit.point.x, _t.position.y, hit.point.z)); } } // If autorun is enabled, set Y input to always be 1 until user uses the Y axis if (_autorun) { if (_input_y == 0) { _input_y = 1; } else { _autorun = false; } } // If user is using strafe keys, override X axis if (_input_s != 0) { _input_x = _input_s; } // If the user is not holding right-mouse button, rotate the player with the X axis instead of strafing if (!Input.GetMouseButton(1) && !mouseLockMovement && _input_x != 0 && _input_s == 0) { _t.Rotate(new Vector3(0, _input_x * (turnSpeed / 2.0f), 0)); _rotation = _input_x; _input_x = 0; } else { _rotation = 0; } // Movement direction and speed if (_input_y < 0) { _speed = backpedalSpeed; } else { if (_running) { _speed = runSpeed; } else { _speed = walkSpeed; } } if (clickToMove) { if (_last_wanted_position != _wanted_position) { float d = Vector3.Distance(_t.position, _wanted_position); if (d > _last_distance) { d = 0; } else { _last_distance = d; } if (d >= 0.1f) { _t.LookAt(new Vector3(_wanted_position.x, _t.position.y, _wanted_position.z)); _input_y = Mathf.Clamp(d / 2f, 0, 1); } else { _last_wanted_position = _wanted_position; _input_y = 0; } } } // If on the ground, test to see if still on the ground and apply movement direction if (_grounded) { if (moveRelativeToCamera) { Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward); forward.y = 0f; forward = forward.normalized; Vector3 right = new Vector3(forward.z, 0.0f, -forward.x); float h = _input_x * input_modifier; float v = _input_y * input_modifier; Vector3 walkDirection = (h * right + v * forward); _velocity = walkDirection * _speed; } else { _velocity = new Vector3(_input_x * input_modifier, -antiBunny, _input_y * input_modifier); _velocity = _t.TransformDirection(_velocity) * _speed; } // Animation _move_speed = (_t.position - _last_position).magnitude; _last_position = _t.position; if (_move_speed > 0) { if (_move_speed > 0.07f) { if ((Input.GetMouseButton(1) || mouseLockMovement) && _input_x != 0) { if (_input_x < 0) { if (_input_y > 0) { _animator.Action = animationRunStrafeLeft45; } else { _animator.Action = animationRunStrafeLeft90; } } else { if (_input_y > 0) { _animator.Action = animationRunStrafeRight45; } else { _animator.Action = animationRunStrafeRight90; } } } else { _animator.Action = animationRun; } _animation_speed = 1; } else { if ((Input.GetMouseButton(1) || mouseLockMovement) && _input_x != 0) { if (_input_x < 0) { if (_input_y > 0) { _animator.Action = animationWalkStrafeLeft45; } else if (_input_y < 0) { _animator.Action = animationWalkStrafeRight45; } else { _animator.Action = animationWalkStrafeLeft90; } } else { if (_input_y > 0) { _animator.Action = animationWalkStrafeRight45; } else if (_input_y < 0) { _animator.Action = animationWalkStrafeLeft45; } else { _animator.Action = animationWalkStrafeRight90; } } } else { _animator.Action = animationWalk; } _animation_speed = _move_speed * 13 + 1; if (_input_y < 0) { _animation_speed = -_animation_speed; } } } else { if (_rotation < 0) { _animator.Action = animationTurnLeft; } else if (_rotation > 0) { _animator.Action = animationTurnRight; } else { _animator.Action = animationIdle; } } if (!Physics.Raycast(_t.position, -Vector3.up, 0.2f)) { _grounded = false; } } else { if (_velocity.y > 0) { _animator.Action = animationJump; } else { _animator.Action = animationFall; } // Sets the falling start position to the highest point the player reaches if (_fall_start < _t.position.y) { _fall_start = _t.position.y; } } _velocity.y -= gravity * Time.deltaTime; _controller.Move(_velocity * Time.deltaTime); }