protected override void SetPositionAndMovement(SpaceRock rock, float movementSpeed) { var movementAngle = UnityEngine.Random.Range(0f, Mathf.PI * 2f); var boundingSphere = new Sphere(Vector3.zero, 0f); var element = GetComponent <SpriteElement>(); boundingSphere.Encapsulate(((SphereBounds)element.bounds).sphere); rock.transform.localPosition = transform.localPosition + new Vector3(Mathf.Cos(movementAngle), Mathf.Sin(movementAngle), 0f) * boundingSphere.radius * 0.5f; rock.GetComponent <Rigidbody2D>().velocity = new Vector2(Mathf.Cos(movementAngle), Mathf.Sin(movementAngle)) * movementSpeed; }
protected abstract void SetPositionAndMovement(SpaceRock rock, float movementSpeed);
protected override void SetPositionAndMovement(SpaceRock rock, float movementSpeed) { var element = rock.GetComponent <SpriteElement>(); var boundingSphere = new Sphere(Vector3.zero, 0f); boundingSphere.Encapsulate(((SphereBounds)element.bounds).sphere); var radius = boundingSphere.radius * 0.99f; var width = world.untransformedAxis0Vector.magnitude; var height = world.untransformedAxis1Vector.magnitude; var worldSurfaceNormal = Vector3.Cross(world.untransformedAxis1Vector, world.untransformedAxis0Vector).normalized; var axis0Normal = Vector3.Cross(world.untransformedAxis0Vector, worldSurfaceNormal).normalized; var axis1Normal = Vector3.Cross(worldSurfaceNormal, world.untransformedAxis1Vector).normalized; int side = UnityEngine.Random.Range(0, 2) + (UnityEngine.Random.Range(0f, width + height) < width ? 0 : 2); float movementAngle; switch (side) { case 0: // From bottom moving up. rock.transform.localPosition = world.untransformedOrigin + world.untransformedAxis0Vector * UnityEngine.Random.Range(0f, 1f) - axis0Normal * radius; movementAngle = UnityEngine.Random.Range(Mathf.PI * 0.25f, Mathf.PI * 0.75f); break; case 1: // From top moving down. rock.transform.localPosition = world.untransformedOrigin + world.untransformedAxis0Vector * UnityEngine.Random.Range(0f, 1f) + world.untransformedAxis1Vector + axis0Normal * radius; movementAngle = UnityEngine.Random.Range(Mathf.PI * -0.75f, Mathf.PI * -0.25f); break; case 2: // From left moving right. rock.transform.localPosition = world.untransformedOrigin + world.untransformedAxis1Vector * UnityEngine.Random.Range(0f, 1f) - axis1Normal * radius; movementAngle = UnityEngine.Random.Range(Mathf.PI * -0.25f, Mathf.PI * 0.25f); break; case 3: // From right moving left. rock.transform.localPosition = world.untransformedOrigin + world.untransformedAxis1Vector * UnityEngine.Random.Range(0f, 1f) + world.untransformedAxis0Vector + axis1Normal * radius; movementAngle = UnityEngine.Random.Range(Mathf.PI * 0.75f, Mathf.PI * 1.25f); break; default: throw new System.NotImplementedException(); } rock.GetComponent <Rigidbody2D>().velocity = new Vector2(Mathf.Cos(movementAngle), Mathf.Sin(movementAngle)) * movementSpeed; }