Пример #1
0
    /// <summary>
    /// Checks nearby for valid places for the drone to move.
    /// Valid places must be in front of the player, and not intersect any objects within a reasonable tolerance.
    /// Use radiusCheckRate and percentageThreshold to tweak what counts as a valid location.
    /// </summary>
    /// <param name="newpos"></param>
    /// <returns></returns>
    private bool FindNewMovePosition(out Vector3 newpos)
    {
        //We can't move if the ZED isn't initialized.
        if (!zedManager.IsZEDReady)
        {
            newpos = transform.position;
            return(false);
        }

        Vector3 randomPosition;

        // Look Around For a New Position
        //If the Drone is on the screen, search around a smaller radius.
        //Note that we only check the primary ZEDManager because we only want the drone to spawn in front of the player anyway.
        if (ZEDSupportFunctions.CheckScreenView(transform.position, zedManager.GetMainCamera()))
        {
            randomPosition = UnityEngine.Random.onUnitSphere * UnityEngine.Random.Range(2f, 3f) + transform.position;
        }
        else //if the drone is outside, look around a bigger radius to find a position which is inside the screen.
        {
            randomPosition = UnityEngine.Random.onUnitSphere * UnityEngine.Random.Range(4f, 5f) + transform.position;
        }

        // Look For Any Collisions Through The ZED
        bool hit = ZEDSupportFunctions.HitTestAtPoint(zedManager.zedCamera, zedManager.GetMainCamera(), randomPosition);

        if (!hit)
        {
            newpos = transform.position;
            return(false);
        }

        //If we spawn the drone at that world point, it'll spawn inside a wall. Bring it closer by a distance of ClearRadius.
        Quaternion directiontoDrone = Quaternion.LookRotation(zedManager.GetMainCameraTransform().position - randomPosition, Vector3.up);
        Vector3    newPosition      = randomPosition + directiontoDrone * Vector3.forward * ClearRadius;

        //Check the new position isn't too close from the camera.
        float dist = Vector3.Distance(zedManager.GetMainCamera().transform.position, randomPosition);

        if (dist < 1f)
        {
            newpos = transform.position;
            return(false);
        }

        //Also check nearby points in a sphere of radius to make sure the whole drone has a clear space.
        if (ZEDSupportFunctions.HitTestOnSphere(zedManager.zedCamera, zedManager.GetMainCamera(), newPosition, 1f, radiusCheckRate, percentageThreshold))
        {
            newpos = transform.position;
            return(false);
        }

        //Return true if it's made it this far and out the location we chose.
        newpos = newPosition;
        return(true);
    }
    /// <summary>
    /// Spawns the drone.
    /// </summary>
    /// <returns>The drone.</returns>
    /// <param name="spawnPosition">Spawn position.</param>
    public Drone SpawnDrone(Vector3 spawnPosition)
    {
        //Spawn the drone
        GameObject dronego        = Instantiate(dronePrefab);
        Drone      dronecomponent = dronego.GetComponentInChildren <Drone>();

        if (!dronecomponent)
        {
            UnityEngine.Debug.LogError("Drone prefab spawned by DroneSpawner does not contain the Drone.cs component.");
        }

        //Give the drone a reference to this object so it can clear its reference and set the timer properly when it dies
        dronecomponent.SetMySpawner(this);

        //Set the drone's transform values
        dronego.transform.position = newspawnposition;                                                                                  //Assign the random Pos generated in CheckRandomSpawnLocation();
        dronego.transform.rotation = Quaternion.LookRotation(zedManager.GetMainCameraTransform().position - spawnPosition, Vector3.up); //Make it look at the player.

        return(dronecomponent);
    }
    private void Update()
    {
        Vector3 moveAxis      = Vector3.zero; //Translation. Used by keyboard only.
        float   inputRotation = 0f;           //Applied rotation, between -1 and 1. Cumulative between keyboard and controllers.
        float   inputScale    = 0f;           //Applied scale change, either -1, 0 or 1. Cumulative between keyboard and controllers.

        //Keyboard inputs.
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Q))
        {
            inputRotation = -1 * (rotationSpeed * 360) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.E))
        {
            inputRotation = 1 * (rotationSpeed * 360) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            moveAxis = Vector3.forward * movementSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
        {
            moveAxis = Vector3.back * movementSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.A))
        {
            moveAxis = Vector3.left * movementSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))
        {
            moveAxis = Vector3.right * movementSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.R))
        {
            moveAxis = Vector3.up * movementSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.F))
        {
            moveAxis = Vector3.down * movementSpeed * Time.deltaTime;
        }

        Quaternion gravity = Quaternion.identity;


        if (moveAxis != Vector3.zero)
        {
            isMoving = true;
            if (motion == RelativeMotion.Itself)
            {
                transform.Translate(moveAxis.x, moveAxis.y, moveAxis.z);
            }
            else if (motion == RelativeMotion.Camera)
            {
                gravity = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
                transform.localPosition += zedManager.GetMainCameraTransform().right *moveAxis.x;
                transform.localPosition += zedManager.GetMainCameraTransform().forward *moveAxis.z;
                transform.localPosition += gravity * zedManager.GetMainCameraTransform().up *moveAxis.y;
            }
        }
        else
        {
            isMoving = false;
        }

        if (Input.GetKey(KeyCode.Mouse0))
        {
            inputScale = 1f;
        }
        else if (Input.GetKey(KeyCode.Mouse1))
        {
            inputScale = -1f;
        }

        if (zedManager)
        {
            Vector3 moveaxis = new Vector3(); //Position change by controller. Added to keyboard version if both are applied.

            //Looks for any input from this controller through SteamVR.
            //Left controller controls rotation and increasing scale.
            if (objectTrackers.Count > 0)
            {
                moveaxis = objectTrackers[0].CheckNavigateUIAxis();
                if (Mathf.Abs(moveaxis.x) < 0.1f)
                {
                    moveaxis.x = 0;                               //Add slight deadzone.
                }
                if (Mathf.Abs(moveaxis.y) < 0.1f)
                {
                    moveaxis.y = 0;
                }
                inputRotation += moveaxis.x * rotationSpeed * 360f * Time.deltaTime;

                gravity = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
                transform.localPosition += gravity * zedManager.GetMainCameraTransform().up *moveaxis.y *movementSpeed *Time.deltaTime;

                if (objectTrackers[0].CheckClickButton(ControllerButtonState.Held))
                {
                    inputScale = 1f;
                }
            }
            //Right controller controls translation and lowering scale.
            if (objectTrackers.Count > 1)
            {
                moveaxis = objectTrackers[1].CheckNavigateUIAxis();
                if (Mathf.Abs(moveaxis.x) < 0.1f)
                {
                    moveaxis.x = 0;                               //Add slight deadzone.
                }
                if (Mathf.Abs(moveaxis.y) < 0.1f)
                {
                    moveaxis.y = 0;
                }
                if (moveaxis.x != 0 || moveaxis.y != 0)
                {
                    isMoving = true;
                    gravity  = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
                    transform.localPosition += zedManager.GetMainCameraTransform().right *moveaxis.x *movementSpeed *Time.deltaTime;
                    transform.localPosition += gravity * zedManager.GetMainCameraTransform().forward *moveaxis.y *movementSpeed *Time.deltaTime;
                }
                else
                {
                    isMoving = false;
                }

                if (objectTrackers[1].CheckClickButton(ControllerButtonState.Held))
                {
                    inputScale = -1f;
                }
            }
        }

        //Rotation
        float h = inputRotation;

        if (invertRotation)
        {
            transform.Rotate(0, h, 0);
        }
        else
        {
            transform.Rotate(0, -h, 0);
        }

        //Scale
        float s = scaleSpeed * (inputScale * Time.deltaTime);

        transform.localScale = new Vector3(transform.localScale.x + s,
                                           transform.localScale.y + s,
                                           transform.localScale.z + s);

        if (transform.localScale.x > maxScale)
        {
            transform.localScale = new Vector3(maxScale, maxScale, maxScale);
        }
        else if (transform.localScale.x < minScale)
        {
            transform.localScale = new Vector3(minScale, minScale, minScale);
        }

        //Enable/disable light if moving.
        if (spotLight != null)
        {
            SetMovementLight();
        }
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        string[] names       = Input.GetJoystickNames();
        bool     hasJoystick = false;

        if (names.Length > 0)
        {
            hasJoystick = names[0].Length > 0;
        }


        /// Adjust Planetarium X/Y/Z position
        float axisH = Input.GetAxis("Horizontal");
        float axisV = Input.GetAxis("Vertical");

        Quaternion gravity = Quaternion.identity;

        gravity = Quaternion.FromToRotation(manager.GetZedRootTansform().up, Vector3.up);
        planetarium.transform.localPosition += manager.GetMainCameraTransform().right *axisH *speedMove *Time.deltaTime;
        planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().forward *axisV *speedMove *Time.deltaTime;

        /// Adjust Scale of Virtual objects,lights, sounds
        bool ScaleUpButton   = Input.GetButton("Fire1") || Input.GetKey(KeyCode.JoystickButton5) || (Input.GetAxis("Fire1") >= 1);
        bool ScaleDownButton = Input.GetButton("Fire2") || (Input.GetAxis("Fire2") >= 1);

        currentscale += System.Convert.ToInt32(ScaleUpButton) * speedGrowth * Time.deltaTime / scaler;
        currentscale -= System.Convert.ToInt32(ScaleDownButton) * speedGrowth * Time.deltaTime / scaler;
        if (currentscale < MIN_LIMIT_SCALE)
        {
            currentscale = MIN_LIMIT_SCALE;
        }
        if (currentscale > MAX_LIMIT_SCALE)
        {
            currentscale = MAX_LIMIT_SCALE;
        }
        planetarium.transform.localScale = new Vector3(currentscale, currentscale, currentscale);
        sunlight.range     = currentlightrange * currentscale;
        spotLightSun.range = currentlightrangesunspot * currentscale;
        halolightsun.range = currentlightrangesunhalo * currentscale;

#if __ENABLE__SOUND__
        jupiterSound.maxDistance = currentMaxSoundDistanceJupiter * currentScale;
        sunSound.maxDistance     = currentMaxSoundDistanceSun * currentScale;
#endif

        /// Adjust Rotation of Planetarium
        if (CheckAxes("DPad X") && hasJoystick)
        {
            float axisX = Input.GetAxis("DPad X"); //multiply by 10 since sensibility is at 0.1 by default
            planetarium.transform.Rotate(gravity * manager.GetMainCameraTransform().up *axisX *speedRotation, Space.World);
        }
        else
        {
            float axisX = System.Convert.ToInt32(Input.GetKey(KeyCode.R));
            planetarium.transform.Rotate(gravity * manager.GetMainCameraTransform().up *axisX *speedRotation, Space.World);
        }


        //adjust Height of Planetarium
        if (CheckAxes("DPad Y") && hasJoystick)
        {
            float axisY = Input.GetAxis("DPad Y");
            planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().up *axisY *speedMove *Time.deltaTime;
        }
        else
        {
            float axisY = System.Convert.ToInt32(Input.GetKey(KeyCode.PageUp)) - System.Convert.ToInt32(Input.GetKey(KeyCode.PageDown));
            planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().up *axisY *speedMove *Time.deltaTime;
        }
    }