Revival Jam Day 2 (and some Day 3)


Yesterday was Day 2, and unfortunately the day job consumed most of my day. So, just before bed I decided to spend a few minutes to accomplish at least SOMETHING. I decided to tackle how the elements of the world are instantiated. In my current iteration, trees, rocks and other environmental elements all appear with a position.y value of 0. The ground itself is a procedurally generated mesh using perlin noise so that it's not all flat. But, I couldn't make it too hilly, or else trees and rocks would appear sunk into the ground.

So, the task it to calculate the appropriate height of each environment element, which I've been call "decor". First, I thought the best would be to sample the same perlin noise map, and just grab the value from there - but for the life of me, I couldn't figure that out. It was last, mind you. I then tried using raycasts on each decor to shoot down, find the height of the ground, and reposition there. But that wouldn't work either. I tried all sorts of things. Next thing I know, it's 2:30AM. WTF?

This morning, when I woke up, I realized the problem - the raycast physics wasn't working, because the scene physics hadn't been updated yet. I'm generating the entire level on the first frame. My solution?

Physics.Simulate(Time.fixedDeltaTime);

That'll do it! Except, nope... not that. I figured for sure it would.

On a Unity forum, I found a post from 2014 on a similar topic, suggesting moving the function into a coroutine, and start with yield return new WaitForFixedUpdate(); and go from there. It did the trick! Here's my code:

IEnumerator AdjustHeights()
    {
        yield return new WaitForFixedUpdate();
        decorations = GetComponentsInChildren<decorobject>();
        foreach (DecorObject decorObject in decorations)
        {
            Vector3 origin = decorObject.transform.position;
            origin.y += 100f;
            int layerMask = 1 << 10;
            Ray ray = new Ray(origin, Vector3.down);
            if (Physics.Raycast(ray, out RaycastHit hit, 1000f, layerMask))
            {
                decorObject.transform.position = hit.point;
            }
        }
    }
    

And now, I have trees and rocks et cetera, appearing on far more uneven terrain. At the same time, I ensure that every object will choose a random rotation on the Y axis. It's looking much closer to what I imagined!



On thing that's still bothering me, is how some of the rocks and objects aren't aligned with the ground. All it took was adding a boolean to my DecorObject script, and the following few lines, and presto! My rocks are aligned:

if(decorObject.alignBaseToGround)
                {
                    decorObject.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * decorObject.transform.rotation;
                }

And now I'm aligned!




By the way, here's a link to a Google Document with my to-do list and such pertaining to my goals for this game.

https://docs.google.com/document/d/1NaroplM-ZSrdfVKZmrjoqIiDsvIVVty3Z5OPpQvkSa4/...

Get Moose on the Loose

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.