// Update is called once per frame
    void Update()
    {
        //movement input
        Vector3 moveInput    = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        Vector3 moveVelocity = moveInput.normalized * moveSpeed;

        controller.Move(moveVelocity);

        //look input
        Ray   ray   = viewCamera.ScreenPointToRay(Input.mousePosition);
        Plane plane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight);
        float rayDistance;

        if (plane.Raycast(ray, out rayDistance))
        {
            Vector3 point = ray.GetPoint(rayDistance);
//			Debug.DrawLine( ray.origin, point, Color.red );
            controller.LookAt(point);
            crossHair.transform.position = point;
            crossHair.DetectTargets(ray);

            //square magnitude is faster to compute
            float sqrDistance = (new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude;
            //print( distance );
            if (sqrDistance > Mathf.Pow(aimThreshold, 2))
            {
                gunController.Aim(point);
            }
        }

        //weapon input
        if (Input.GetMouseButton(0))
        {
            gunController.OnTriggerHold();
        }
        else if (Input.GetMouseButtonUp(0))
        {
            gunController.OnTriggerRelease();
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            gunController.Reload();
        }

        if (transform.position.y < -10)
        {
            TakeDamage(health);
        }
    }
Beispiel #2
0
    void Update()
    {
        // The code below allows the player's "eyes" to follow the mouse movement.
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        //  Debug.DrawLine(ray.origin, point, Color.red);

        // Emulate the playing surface by creating a new, flat plane. This simplifies the code as we
        // don't have to depend on the actual in-game plane.
        //
        // `Vector3.up` Shorthand for writing `Vector3(0, 1, 0)`.
        Plane plane = new Plane(Vector3.up, Vector3.up * weaponController.weaponHold.position.y);
        float rayLength;

        // This function sets enter to the distance along the ray, where it intersects the plane. If the
        // ray is parallel or pointing in the opposite direction to the plane then `Raycast()` is false
        // and `rayLength` is 0 and negative, respectively.
        if (!plane.Raycast(ray, out rayLength))
        {
            return;
        }

        const int threshold   = 6;
        Vector3   point       = ray.GetPoint(rayLength);
        Vector2   newPoint    = new Vector2(point.x, point.z);
        Vector2   newPosition = new Vector2(transform.position.x, transform.position.z);

        playerController.LookAt(point);

        if ((newPoint - newPosition).sqrMagnitude > threshold)
        {
            weaponController.Aim(point);
        }

        crossHair.transform.position = point;
        crossHair.DetectTargets(ray);

        ScanInput();
        Move();
    }