public void Turn(float amount, bool clockwise) { Vector3 rotation = Vector3.zero; rotation.y += (amount * Time.deltaTime); m_avatar.CharTurning(clockwise); this.gameObject.transform.Rotate(rotation, Space.World); }
void UpdateSmoothedMovementDirection() { Transform cameraTransform = Camera.main.transform; bool grounded = IsGrounded(); //Forward vector relative to the camera along the x-z plane Vector3 forward = cameraTransform.TransformDirection(Vector3.forward); forward.y = 0; forward = forward.normalized; //Right vector relative to the camera //Always orthogonal to the forward vector Vector3 right = new Vector3(forward.z, 0, -forward.x); float v = Input.GetAxisRaw("Vertical"); float h = Input.GetAxisRaw("Horizontal"); //Are we moving backwards or looking backwards? if (v < -0.2f) { movingBack = true; } else { movingBack = false; } bool wasMoving = isMoving; isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f; //Target direction relative to the camera Vector3 targetDirection = h * right + v * forward; if (Input.GetKey(KeyCode.Q)) { controller.gameObject.transform.Rotate(Vector3.up, -45f * Time.deltaTime); charAvatar.CharTurning(false); } if (Input.GetKey(KeyCode.E)) { controller.gameObject.transform.Rotate(Vector3.up, 45f * Time.deltaTime); charAvatar.CharTurning(true); } //Grounded controls if (grounded) { //Lock camera for short period when transitioning moving & standing still lockCameraTimer += Time.deltaTime; if (isMoving != wasMoving) { lockCameraTimer = 0.0f; } //We store speed and direction seperately, //so that when the character stands still we still have a valid forward direction //moveDirection is always normalized, and we only update it if there is user input. if (targetDirection != Vector3.zero) { //If we are really slow, just snap to the target direction if (moveSpeed < walkSpeed * 0.9f && grounded) { moveDirection = targetDirection.normalized; } //Otherwise smoothly turn towards it else { moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000f); moveDirection = moveDirection.normalized; } if (grounded) { charAvatar.SetCurGroundHeight(this.gameObject.transform.position.y); } } //Smooth the speed based on the current target direction float curSmooth = speedSmoothing * Time.deltaTime; //Choose target speed //We want to support analog input but make sure you can't walk faster diagonally than just forward or sideways float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f); //Pick speed modifier if (Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift)) { targetSpeed *= runSpeed; } else { targetSpeed *= walkSpeed; } moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth); } //In air controls else { //Lock camera while in air if (jumping) { lockCameraTimer = 0.0f; } if (isMoving) { inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration; } } }