/// <summary> /// This function is called every fixed framerate frame /// Here we take care of enabling & disabling the laser pointer. /// </summary> private void FixedUpdate() { //If we have been moved by the baseball bat if (IsMoving) { //Look for our next position based on our current velocity. Vector3 predictedPos = centerpoint.position + (rb.velocity * (Time.deltaTime * 2.5f)); transform.rotation = Quaternion.LookRotation(rb.velocity.normalized); //Collision check with the real world at that next position. if (ZEDSupportFunctions.HitTestAtPoint(leftcamera, predictedPos)) { //We hit something, but is it a flat surface? if (planeManager.DetectPlaneAtHit(leftcamera.WorldToScreenPoint(predictedPos))) { bunnyspawner.SpawnUI(predictedPos); IsMoving = false; } else//If not, bounce off of it but still show the flag. { IsMoving = false; //Not moving anymore, so update our state. bunnyspawner.SpawnUI(predictedPos); //Start spawning the UI on our current location. rb.velocity = Vector3.Reflect(rb.velocity / 2, transform.forward); //Bounce off the surface we hit } } } }
/// <summary> /// This function is called every fixed framerate frame /// Here we take care of enabling & disabling the laser pointer. /// </summary> private void FixedUpdate() { //If we have been moved by the baseball bat if (_moving) { //Look for our next position based on our current velocity. Vector3 predictedPos = _centerPoint.position + (_rb.velocity * (Time.deltaTime * 2.5f)); transform.rotation = Quaternion.LookRotation(_rb.velocity.normalized); //Collision check with the real world at that next position. if (ZEDSupportFunctions.HitTestAtPoint(_leftCamera, predictedPos)) { //We hit something, but is it a flat surface? if (_zedPlane.DetectPlaneAtHit(_leftCamera.WorldToScreenPoint(predictedPos))) { _mySpawner.SpawnUI(predictedPos); _moving = false; } else//If not freeze on hit. { //_rb.isKinematic = true; //Freeze the object at the current position. _moving = false; //Not moving anymore, so update our state. _mySpawner.SpawnUI(predictedPos); //Start spawning the UI on our current location. _rb.velocity = Vector3.Reflect(_rb.velocity / 2, transform.forward); } } } }
/// <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.GetLeftCamera())) { 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.GetLeftCamera(), 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.GetLeftCameraTransform().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.GetLeftCamera().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.GetLeftCamera(), 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); }