コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        // More responsive movement
        moveInput    = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
        moveVelocity = moveInput * moveSpeed;

        //camera to mouse interaction
        Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
        //create another plane
        Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
        float rayLength;

        //raycast (determine lenght of particular ray; if camera hits anything else in our world)
        if (groundPlane.Raycast(cameraRay, out rayLength))
        {
            Vector3 pointToLook = cameraRay.GetPoint(rayLength);
            Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);

            //make player look at what we (the mouse) is pointing at. Does this for crosshairs as well
            transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
            crosshairs.transform.position = new Vector3(pointToLook.x, transform.position.y, pointToLook.z);
            crosshairs.Detect(cameraRay);
        }

        //left mouse button to fire
        if (Input.GetMouseButtonDown(0))
        {
            theGun.isFiring = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            theGun.isFiring = false;
        }
    }