2
\$\begingroup\$

I have created a scene in Unity3D that's filled with spheres. I want to be able to move forward forever and still be passing through the spheres.

I was thinking I'd keep the player in one spot and move the spheres. Maybe destroy the previous bunch of spheres and create a new bunch in random coordinates? I really don't have idea how to start.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site, I think it's not clear what are you trying to ask. Maybe you can rephrase your question so people can really understand your question? \$\endgroup\$ – concept3d Dec 2 '13 at 10:50
  • \$\begingroup\$ This sounds very much like a problem I've had to conquer in the past - how to randomly generate an endless space of 'stuff', which updates as you move through it, without needing to store an infinite amount of information. The question could definitely use some better clarification though. \$\endgroup\$ – MrCranky Dec 2 '13 at 11:47
2
\$\begingroup\$

Sounds like you are looking for something like a sliding window effect, centred around the camera position. I'm going to gloss over how you're spawning the spheres initially, because that will very much depend on how your scene is setup, but rather than destroying and creating spheres it may be more efficient to simply move them to where you want them to be. Here's some off-the-cuff C# code that should get you started. It assumes that you have a list of spheres which is pre-populated with a bunch of GameObjects that you're going to render. Also key is knowing where the camera moved from to where it is now (because you need that information to decide where you're going to spawn the new spheres.

Vector3 previousCameraPosition;    
void Update()
{
    Vector3 cameraPosition = camera.transform.position;
    Vector3 movement = cameraPosition - previousCameraPosition;
    const float kInterestRadius = 1000.0f;

    List<GameObject> spheresToPrune = new List<GameObject>();
    foreach (GameObject sphere in spheres)
    {
        Vector3 spherePosition = sphere.transform.position;
        float distanceToCamera = Vector3.Distance(cameraPosition, spherePosition);
        bool isWithinCameraSphere = (distanceToCamera < kInterestRadius);

        if (!isWithinCameraSphere)
        {
            spheresToPrune.Add(sphere);
        }
    }

    //Take all the spheres that fell out of the back of the moving window,
    // and shift them to random positions near the front of the window.
    // Since we're sweeping a sphere, that means the front hemisphere.
    // Easiest way to pick a random point on a hemisphere is through polar 
    // co-ordinates
    foreach (GameObject sphere in spheresToPrune)
    {
        // Azimuth of 0 and inclination of 0 == straight forward in the direction of camera motion
        // -90 degrees azimuth == left from camera motion
        // 90 degrees elevation == up from camera motion

        //Pick an azimuth in the front 180 degree sweep
        float azimuth = UnityEngine.Random.Range(-90.0f, 90.0f);
        //Pick random inclination
        float inclination = UnityEngine.Random.Range(-90.0f, 90.0f);

        // NB: you may have to juggle the parameter order
        // so that the rotation correctly transforms the position as you expect 
        // in your own coordinate system
        Quaternion rotation = Quaternion.Euler(azimuth, inclination, 0.0f);

        // Alter the camera movement vector by the rotation so that it points towards the new position
        Vector3 rotatedMovement = (rotation * movement);

        // Push the direction out to the very edge of the sphere
        Vector3 newPosition = rotatedMovement * kInterestRadius;

        //Shift the sphere that was pruned to its new position
        //Alternately, destroy the pruned sphere (calling spheres.Remove to
        // delete it from the list of spheres), and then create a new one in the new position
        sphere.transform.localPosition = newPosition;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.