void Update() { //Movement Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); moveInput = playerCamera.transform.TransformDirection(moveInput); moveInput.y = 0; Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //Rotation Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gun.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTarget(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gun.Aim(point); } } //Weapon if (Input.GetMouseButton(0)) { gun.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gun.OnTriggerReleased(); } if (Input.GetKeyDown(KeyCode.R)) { gun.Reload(); } //Camera if (Input.GetKey(KeyCode.Q)) { camCtrl.Rotate('Q'); } if (Input.GetKey(KeyCode.E)) { camCtrl.Rotate('E'); } //Bounds if (transform.position.y < -20) { TakeDamage(health); } }
void Update() { //Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //Rotation input Ray ray = camera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); controller.LookAt(point); crosshair.transform.position = point; crosshair.DetectTarget(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).magnitude > 1) { gunController.Aim(point); } } //Weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } }
/// <summary> /// Handles the player rotation during the update phase. This represents where we want the player to look. /// </summary> private void HandlePlayerRotation() { // show a ray from the camera down to the mouse position on the ground Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); // we don't need a reference to the ground in the scene; we instead can generate one programmatically and that can serve as the ground Plane groundPlane = new Plane( Vector3.up, // pass in the normal of the plane, which is a direction perpendicular to a plane that is lying flat Vector3.up * gunController.GunHeight // the endpoint doesn't matter ); // if the ray intersects (i.e. hit) the ground plane, then we'll know the length from camera to intersection (i.e. ray distance) float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { // actual point of intersection Vector3 point = ray.GetPoint(rayDistance); // draw a line in the scene view showing where the player will be looking if (debug) { Debug.DrawLine(ray.origin, point, Color.red); } // rotate the player to look at the point playerController.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTarget(ray); // debug for finding distance between crosshair dot point and player (to determine when crosshairs are too close) // squared magnitude is faster than magnitude float distance = (new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude; if (distance > 1.21f) { // better aiming to crosshairs gunController.Aim(point); } } }
// Update is called once per frame void Update() { // Movement Vector3 direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 velocity = direction.normalized * moveSpeed; controller.Move(velocity); // Look at mouse position Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.forward * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshair.transform.position = point; crosshair.DetectTarget(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } // Weapon shoots if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } // Stop shooting if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerReleased(); } //Reload the gun if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } // GOD mode, auto shoot, no need for holding mouse button if (Input.GetKeyDown(KeyCode.G)) { StartCoroutine(AutoShoot()); } if (transform.position.y < -5) { TakeDamage(health); } }