/// <summary> /// For each level, this method creates a set of game objects whose /// transform positions mark the bounce points of the character during an ideal trajectory toward home. /// Position of home is always set to the last item in this array. /// </summary> public void CreateRandomTargetPositions(Vector3 startingHit, Vector3 startingDirection, Vector3 startingNormal, int numNewBounces) { _startPosition = startingHit; _startNormal = startingNormal; _startApproach = startingDirection; _bounceCount = numNewBounces; GameObject dummyController = new GameObject(); // raycast to first hit Vector3 currentHitPos = startingHit; // new Vector3(Random.Range(0,3.0f), Random.Range(0, 3.0f), Random.Range(0, 3.0f)); Vector3 currentHitNormal = new Vector3(Random.Range(0, 3.0f), Random.Range(0, 3.0f), Random.Range(0, 3.0f)); Vector3 approachDirection = startingDirection; // currentHitPos - dummyController.transform.position; BounceRaycastController rayComponent = _controller.AddComponent(typeof(BounceRaycastController)) as BounceRaycastController; rayComponent.OnRaycastResult.AddListener(OnRaycastHit); //IterateBounce(approachDirection, currentHitNormal); StartCoroutine(CheckHit()); // calc angle of approach - compare to hit normal to find departure angle //start from current controller position // cast a ray at a random rotation // if it hit world mesh, start with that point, else try again // get the vector direction by subtracting start from destination // // randomly choose whether to instantiate a target // use the ratio of num targets to bounces to determine likelihood a target gets created at this bounce // track total num targets created - if numNewTargets - totalTargetsCreated == numNewBounces - numBouncesCreated // then stop randomly choosing whether to create a target and just create one for each remaining // create a game object for the current bounce if appropriate and orient forward vector along hit normal // add metadata to the game object (approach angle, departure angle, hasTarget, etc.) _hitCheck = 0; }
private void IterateBounce(Vector3 newHitPos, Vector3 approachDirection, Vector3 currentHitNormal) { Vector3 departureDirection = Vector3.Reflect(approachDirection, currentHitNormal); GameObject newHit = new GameObject(); newHit.transform.position = newHitPos; Bounce bounce = newHit.AddComponent(typeof(Bounce)) as Bounce; bounce.departureDirection = departureDirection; bounce.approachDirection = approachDirection; // Instantiate debug visualizer objects here and attach them as children to newHit GameObject dInstance = Instantiate(_debugInstance, newHit.transform.position, newHit.transform.rotation); dInstance.transform.SetParent(newHit.transform); bounces.Add(newHit); if (_bounceCount < _bounceTotal - 1) { _raycastController.transform.position = newHit.transform.position; _raycastController.transform.rotation = Quaternion.FromToRotation(_raycastController.transform.forward, departureDirection); _rayComponent = newHit.AddComponent(typeof(BounceRaycastController)) as BounceRaycastController; _rayComponent.OnRaycastResult.AddListener(OnRaycastHit); } _bounceCount++; }
IEnumerator AddRaycastListener(BounceRaycastController _rayComponent) { yield return(new WaitForSeconds(1)); _rayComponent.OnRaycastResult.AddListener(OnRaycastHit); }