Procedurally generated terrain for snowboarded

Here’s my code till now

public SpriteShapeController shape;
    
    public int distBtwPoints = 7;
    int i = 50;
    int x = 1;
    public int height=5;

    private void Start() {
        shape = GetComponent<SpriteShapeController>();
        
        for (int i =0; i<50; i++){
            shape.spline.SetPosition(i+2,shape.spline.GetPosition(i+2) + Vector3.right*distBtwPoints + Vector3.down*1);
            shape.spline.SetPosition(i+3,shape.spline.GetPosition(i+3) + Vector3.right*distBtwPoints + Vector3.down*1);
        
        
            float xPos = shape.spline.GetPosition(i+1).x + distBtwPoints;
            shape.spline.InsertPointAt(i + 2, new Vector3(xPos,height*Mathf.PerlinNoise(i*Random.Range(5f,15f),0) - i));

            shape.spline.SetTangentMode(i+2,ShapeTangentMode.Continuous);
            shape.spline.SetLeftTangent(i+2, new Vector3(-4,0,0));
            shape.spline.SetRightTangent(i+2, new Vector3(4,0,0));

             
        }
        InvokeRepeating("Generate",5,0.3f);
        
    }

    private void Generate() {
        shape.spline.RemovePointAt(1);
        shape.spline.SetPosition(0,shape.spline.GetPosition(1)+Vector3.down*100);
        
            
        shape.spline.SetPosition(i+2-x,shape.spline.GetPosition(i+2-x) + Vector3.right*distBtwPoints + Vector3.down*1);
        shape.spline.SetPosition(i+3-x,shape.spline.GetPosition(i+3-x) + Vector3.right*distBtwPoints + Vector3.down*1);


        float xPos = shape.spline.GetPosition(i+1-x).x + distBtwPoints;
        shape.spline.InsertPointAt(i + 2-x, new Vector3(xPos,height*Mathf.PerlinNoise((i-x)*Random.Range(5f,15f),0) - (i)));

        shape.spline.SetTangentMode(i+2-x,ShapeTangentMode.Continuous);
        shape.spline.SetLeftTangent(i+2-x, new Vector3(-4,0,0));
        shape.spline.SetRightTangent(i+2-x, new Vector3(4,0,0));
        
        i++;
        x++;
        
    }

I’ve generated a big mesh having 50 nodes in the start method and then in the generate method im simultaneosly deleting and adding 1 node at a time so that the mesh doesnt’t take up too much space.

Now here’s the issue. Im using invokerepeating to call the method so my entire code depends on whether the speed of the snowboarder and the speed of the node deletion is same. If not, one of them would get too ahead of the other and eventually the snowboarder will have nothing below him.

Here’s what i wanna do. I want to create a trigger parented to the snowboarder and when a node touches the trigger it gets deleted. Any idea on how to do it?

Hi,

First of all, good job on developing your own solution and on describing your idea as well as the problem you’d like to solve. :slight_smile:

Secondly, you are right about InvokeRepeating. The player’s speed on the x-axis is inconsistent. For this reason, your current code might generate too new terrain too often, which might have a negative impact on the performance down the road. Generating it only if needed if a good idea to increase the performance.

You do it exactly as you described it. Make the trigger collider large enough so your node won’t miss it. In your code, you use OnTriggerEnter2D to detect the moment when the node touches the trigger collider. You might also want to delete the old terrain part because the player is not able to go back anyway. Otherwise, you might end up with a giant scene if your future players enjoy your game too much. :wink:


An alternative idea would be to make the world generation depend on the player’s position. Each tile takes up 1 World Unit (WU), and the player’s position is also in WU. Of course, you need an offset to generate the subsequent part before the player/camera reaches those world coordinates. Do not work with exact position values, though, because it is unlikely that the player will be, for example, at exactly x = 15f instead of x = 15.0021f.

Thank you!

I just dont know how to find the node which is colliding with the trigger. OnTriggerEnter would refer to the entire shape profile wouldnt it

What is a “node”? Could you sketch your ideas with the relevant objects?

I thought your idea was something like that:

image

by ‘node’ i mean the points on the spline. I need to delete those points. And yes my idea is like that only except the trigger collider is behind the player (off camera) so when the shape touches the collider it deletes that node.

Actually i think I solved it. I should just delete the point at index 1 when it collides. I dont know why i was having trouble figuring out the index of the point which is colliding with the trigger

Good job! :slight_smile:

There is indeed a method named RemovePointAt in the Spline class.


See also:

I have one more problem actually lol. How do i CREATE the node using the trigger? Wouldnt the trigger always be colliding with the shape if i have it front of my player?

In my sketch, the trigger is supposed to be a control point. It is not part of the player.

If you don’t know how to work with the Spline, I would suggest to create a test scene with a test class where you try to generate a simple line, then a zigzag line, then a more complex shape with different hard-coded coordinates. Once you did that, try to modify the generated shape via code.

If that worked, you could simply apply your knowledge to your current solution. Don’t try to manipulate the shape in your scene because your scene is “too” complex.

This is probably what you need to generate a (simple) splite and read the generated data at runtime:

https://docs.unity3d.com/Packages/com.unity.splines@1.0/manual/spline-container.html
https://docs.unity3d.com/Packages/com.unity.splines@2.0/api/UnityEngine.Splines.Spline.html
https://docs.unity3d.com/Packages/com.unity.splines@2.0/api/UnityEngine.Splines.BezierKnot.html

It works now thanks. I was actually thinking of creating 2 triggers, one for destroying and one for creating. This is useless as I should create a point as soon as i delete one, so the destroyer trigger is enough. Just posting this in case someone from the future wants to make an endless level

Good job on realising your idea! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms