Example #1
0
    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);
        }
    }