• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Arnold // Golden Donkey Productions
      Hi, I've been working on this issue for a while and haven't yet found an answer.
      Does anyone know the best way to convert unity's LAT & LONG into a vector 3 position that I could use in a virtual world (if it's even possible). 
      Thankyou in advance
    • By wobbegong_
      I am taking an absolute beginner's game development course and we have just finished game jams in small groups. Our current assignment is to get feedback from people working in any aspect of game development. I would very much appreciate any feedback! The game is up on itchi.io (sound warning) https://wobbegong.itch.io/zombie-shooter It's essentially a very basic PvE.
      I also have some things I'm wondering about, but you don't necessarily have to answer these. 
      1. Do you have any tips on working with physics? My group wrestled a bit with Rigidbody physics not totally working the way we wanted to -- jumping ended up kind of floaty and inclines seem to mess up movement. Alternatively... how can I build terrains with depth that won't result in wonky physics?
      2. How can I keep up the level of challenge in an interesting way as the player progresses through the waves?
      3. What are some of your personal guidelines for creating title screens?
      Thank you very much in advance!
    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
       
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Loading outward from a point

This topic is 706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I've set up a script that finds a players position relative to the chunks that make up my world. I was wondering If there was a way to get from a center point, and load outwards from the player - something like layers.

 

Code for those who are wondering:

using UnityEngine;
using System.Collections;

[System.Serializable()]

public class LoadChunks : MonoBehaviour {

    public Planet planet;
    public bool finished = true;

    public float planetx;
    public float planety;
    public float planetz;

    public float x;
    public float y;
    public float z;

    float lastx;
    float lasty;
    float lastz;

    public float length;

    public float Range = 32;
	
	// Update is called once per frame
	public void Update () {

        if (finished == true)
        {
            if (planet == null)
            {
                GetPlanet();
                Vector3 pos = planet.transform.position;
                planetx = pos.x;
                planety = pos.y;
                planetz = pos.z;
                length = planet.planetSize / 16;
            }

            //Getting player position
            x = this.transform.position.x;
            y = this.transform.position.y;
            z = this.transform.position.z;

            //Check if the player has moved, skip a few calculations if they didn't
            if (x != lastx || y != lasty || z != lastz)
            {
                //Normalizing position with planet position
                x = x - planetx;
                y = y - planety;
                z = z - planetz;

                //Converts into chunk position
                x = Mathf.FloorToInt(x / (float)localVars.chunklength);
                y = Mathf.FloorToInt(y / (float)localVars.chunklength);
                z = Mathf.FloorToInt(z / (float)localVars.chunklength);

            }

            //Make sure the player is within the chunks ranges
            if (x > -Range && y > -Range && z > -Range && x <= length + Range && y <= length + Range && z <= length + Range)
            {

                LoadChunksInRadius(x, y, z);
                lastx = x;
                lasty = y;
                lastz = z;

            }
        }
	}

    void LoadChunksInRadius(float x, float y, float z)
    {
        



    }

    void GetPlanet()
    {
        //TODO: Make this get closest planet once multi-planet functionality is added
        planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<Planet>();
    }

}

Share this post


Link to post
Share on other sites
Advertisement

Hmmm loading chunks is your logic, you get the closest x,y,z position, just load chunks that are close to this poistion.

I seriously can't understand what you want. 

Share this post


Link to post
Share on other sites

Okay, Ill explain in more detail. I calculate where the player is in chunk position, so I know which chunk they are in. So, what I want to do is:

 

1. Load that chunk

2. Load the chunks surrounding that chunk

3. Repeat 2 with the new chunks (imagine an expanding circle)

 

Even more example:

 

4  4  3  4  4

4  3  2  3  4

3  2  1  2  3

4  3  2  3  4

4  4  3  4  4

 

(Numbers in order of loading)

Edited by SpikeViper

Share this post


Link to post
Share on other sites

Is this related to your other thread? Without knowing how you store your chunks I can't effectively suggest a way to load chunks. Are we talking file io, coordinate seeded procedural generation, an STL container?

 

edit> what do you mean by 'load'?

Edited by coope

Share this post


Link to post
Share on other sites

Is this related to your other thread? Without knowing how you store your chunks I can't effectively suggest a way to load chunks. Are we talking file io, coordinate seeded procedural generation, an STL container?

 

edit> what do you mean by 'load'?

I have loading set up, I just call planet.load(Position(Its a Vector3)); That loads the chunk. I just need to find the positions to load, preferably going from the player's position outward as explained above.

Share this post


Link to post
Share on other sites

Okay, Ill explain in more detail. I calculate where the player is in chunk position, so I know which chunk they are in. So, what I want to do is:

 

1. Load that chunk

2. Load the chunks surrounding that chunk

3. Repeat 2 with the new chunks (imagine an expanding circle)

 

Even more example:

 

4  4  3  4  4

4  3  2  3  4

3  2  1  2  3

4  3  2  3  4

4  4  3  4  4

 

(Numbers in order of loading)

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

Share this post


Link to post
Share on other sites

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

 

 

I would tend to agree that would be a smart way to do it. Seeing as it is such a useful algorithm in general it makes perfect sense to apply it to any situation it would be warranted and even desired.

 

I would have probably taken to pen and paper to see if I could derive an algorithm from the basic mathematics of your requirements, but Dijkstra is a far better course of action. Far better to use a wheel you've got than to craft a new one.

Share this post


Link to post
Share on other sites

 

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

 

 

I would tend to agree that would be a smart way to do it. Seeing as it is such a useful algorithm in general it makes perfect sense to apply it to any situation it would be warranted and even desired.

 

I would have probably taken to pen and paper to see if I could derive an algorithm from the basic mathematics of your requirements, but Dijkstra is a far better course of action. Far better to use a wheel you've got than to craft a new one

 

How would I use this algorithm to do what m asking? I see it's used for pathfinding, and I've found C# code samples, but I don't see how to make this work for my problem. Sorry, I'm not very experienced with complex algorithms yet.

Share this post


Link to post
Share on other sites

 

 

How would I use this algorithm to do what m asking? I see it's used for pathfinding, and I've found C# code samples, but I don't see how to make this work for my problem. Sorry, I'm not very experienced with complex algorithms yet.

 

 

 

 

Since you've already got something that works this is just for the sake of answering your question.

 

Dijkstras_progress_animation.gif

 

This image is from the Wikipedia article on the algorithm. Notice how the expansion resembles a growing circle.

I'd recommend you take some time in the future to learn how to program the A* algorithm from scratch. It can be used to solve a myriad of problems not just pathfinding in the sense we think about it.

 

I know a guy who used it to solve a word problem.

The problem he faced was something like this:

What are the fewest changes you can make to get from Word <A> to word <B>.

 

Now I can't recall the restrictions he was presented with, but the point is you can solve word problems with a pathfinding algorithm. To use an algorithm you know, to creatively solve a problem it wasn't meant to solve, requires you to be creative and heavily modify said algorithm. The same sort of thing applies here.

 

First step: learn the algorithm.

Second step: show your code who's boss.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement