/// <summary> ///Returns a SphericalObstacle from the current gameObject /// </summary> /// <param name="gameObject"> /// A game object to create the obstacle from<see cref="GameObject"/> /// </param> /// <returns> /// A SphericalObstacle encompassing the game object<see cref="Obstacle"/> /// </returns> public static Obstacle GetObstacle(GameObject gameObject) { SphericalObstacle obstacle; int id = gameObject.GetInstanceID(); Component[] colliders; float radius = 0.0f, currentRadius; if (!ObstacleCache.ContainsKey(id)) { var obstacleData = gameObject.GetComponent <SphericalObstacleData>(); // If the object provides his own spherical obstacle information, // use it instead of calculating a sphere that encompasses the // whole collider. if (obstacleData != null) { ObstacleCache[id] = new SphericalObstacle(obstacleData.Radius, gameObject.transform.position + obstacleData.Center); } else { colliders = gameObject.GetComponentsInChildren <Collider>(); if (colliders == null) { Debug.LogError("Obstacle '" + gameObject.name + "' has no colliders"); return(null); } foreach (Collider collider in colliders) { if (collider.isTrigger) { continue; } // Get the maximum extent to create a sphere that encompasses the whole obstacle float maxExtents = Mathf.Max(Mathf.Max(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z); /* * Calculate the displacement from the object center to the * collider, and add in the maximum extents of the bounds. * Notice that we don't need to multiply by the object's * local scale, since that is already considered in the * bounding rectangle. */ float distanceToCollider = Vector3.Distance(gameObject.transform.position, collider.bounds.center); currentRadius = distanceToCollider + maxExtents; if (currentRadius > radius) { radius = currentRadius; } } ObstacleCache[id] = new SphericalObstacle(radius, gameObject.transform.position); } } obstacle = ObstacleCache[id] as SphericalObstacle; return(obstacle); }
public void Spawn(SpawnPoint spawnPoint) { if (poolList.Count > 0) { int randIndex = UnityEngine.Random.Range(0, poolList.Count); ObstacleCache obstacle = poolList[randIndex]; obstacle.mCollider.isTrigger = false; obstacle.mTransform.position = spawnPoint.pSpawnPoint + _ObstacleInfos[obstacle.mDataIndex]._SpawnOffset; obstacle.mGameObject.SetActive(true); poolList.Remove(obstacle); activeList.Add(obstacle); } }
public void Update() { if (activeList.Count > 0) { for (int i = 0; i < activeList.Count; i++) { ObstacleCache obstacle = activeList[i]; obstacle.mTransform.position += Vector3.left * speed * Time.deltaTime; } //TODO:Fix bound checking based on movement type ObstacleCache firstObject = activeList[0]; float maxX = firstObject.mTransform.position.x + firstObject.mCollider.bounds.size.x * 0.5f; if (maxX < _WorldBounds.bounds.min.x) { activeList.Remove(firstObject); firstObject.mGameObject.SetActive(false); poolList.Add(firstObject); } } }
void Start() { for (int i = 0; i < _ObstacleInfos.Length; i++) { ObstacleData data = _ObstacleInfos[i]; for (int j = 0; j < data._Count; j++) { GameObject poolObject = new GameObject(); SpriteRenderer spriteRenderer = poolObject.AddComponent <SpriteRenderer>(); spriteRenderer.sortingOrder = data._SpriteOrder; spriteRenderer.sprite = data._Sprite; poolObject.transform.position = Vector3.up * 10000; BoxCollider2D collider = poolObject.AddComponent <BoxCollider2D>(); collider.size *= 0.5f; poolObject.name = data._Name + "_" + j.ToString(); poolObject.SetActive(false); poolObject.transform.SetParent(transform); if (data._ShadowSprite) { GameObject shadowObject = new GameObject(); SpriteRenderer shadowSpriteRenderer = shadowObject.AddComponent <SpriteRenderer>(); shadowSpriteRenderer.sortingOrder = data._SpriteOrder; shadowSpriteRenderer.sprite = data._ShadowSprite; shadowObject.name = "shadow"; shadowObject.transform.position = poolObject.transform.position + data._ShadowOffset; shadowObject.transform.SetParent(poolObject.transform); } ObstacleCache cache = new ObstacleCache(); cache.mGameObject = poolObject; cache.mCollider = collider; cache.mTransform = poolObject.transform; cache.mDataIndex = i; poolList.Add(cache); } } }