예제 #1
0
    void Update()
    {
        Vector3 moveInput    = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
        Vector3 moveVelocity = moveInput.normalized * moveSpeed;

        controller.Move(moveVelocity);

        Ray   ray         = viewCamera.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)).sqrMagnitude > 1)
            {
                gunController.Aim(point);
            }
        }

        if (Input.GetMouseButton(0))
        {
            gunController.OnTriggerHold();
        }
        if (Input.GetMouseButtonUp(0))
        {
            gunController.OnTriggerRelease();
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            gunController.Reload();
        }
    }
예제 #2
0
    void LookAtMouse()
    {
        Ray cameraRay = mainCam.ScreenPointToRay(Input.mousePosition); //creates a ray that goes from the camera to he point, where the mouse currently is
        //A plane is added to specify, where the ray is crossing the plane (an abstract, mathematical plane rather then a gameObject plane
        Plane groundPlane = new Plane(Vector3.up, Vector3.up * weaponController.GunHeight);

        //float rayLength;

        if (groundPlane.Raycast(cameraRay, out float rayLength)) //check, if the ray from the camera hits something else in the world (in this case the groundPlane)
        {
            Vector3 pointToLook = cameraRay.GetPoint(rayLength);
            Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);

            transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));

            crosshair.transform.position = pointToLook;
            crosshair.DetectTarget(cameraRay);

            if ((new Vector2(pointToLook.x, pointToLook.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > .6f)
            {
                weaponController.Aim(pointToLook);
            }
        }
    }