At a Glance

I focused almost exclusively on my coding this week. I checked in on a few blogs and read some articles about game dev, however most of the week I spent heads down, going clickity-clackity on my keyboard. I wanted to complete my basic grid-based movement code this week; which meant tackling collider and up/down (y-axis) issues I had. My breakthrough came about midnight-thirty Saturday morning, and bam my character moved up and down the stairs while colliding with walls. Such a great feeling.

Read on to discover my moving story.

A Little Help From My Friends

Mike@PAUSED had some great posts this week, focused on GDC. While I banged my head against the wall and sometimes right through it (you know because the colliers don’t work), he went through and picked out some good content and wrote about it. He highlighted numerous videos on Day 42: GDC 2019 post, and I added them to my growing list of content to watch.

Earlier in the week I found an asset on the store I want to try out, Trello Bug Tracker Pro. It doesn’t look like Diegod Games keeps it updated so I’ll contact them through Unity to discover the status. I use Trello for all my personal and game dev projects, so if I can find a way to get feedback and bug reports directly into Trello, that would be awesome. I only had a vague notion of what to do with it, that is until Mike@PAUSED posted about Telemetry & Feedback and now Subnautica collected metrics from the earliest stages of development so they could continually improve their game. I am still a ways off from that point but I like to work towards a good goal so I can keep moving forward.

Moving Around, And Now Without Trigonometry

When I started this week I had no clear idea of where I wanted to go, more specifically how I wanted my character to move. I had some vague notions about the movement I wanted to see but I had not truly defined the details. I sat down one day and wrote out a plan to work from. I embedded the Trello card below that describes the my goals and process. Let me know if you see it and can interact with it., the board isn’t public so I’m not sure how well it will work. (Edit 9 Apr 2019 – the embedded Trello card didn’t show up properly so I replaced it with my bulleted list of requirements.)

Character Assumptions

  • is 2m tall, 0.5m wide (radius) (configurable via CapsuleCollider)
  • uses CapsuleCollider
  • uses Rigidbody
    • use gravity=true
    • is kinematic=false
    • interpolate=none
    • collision detection=discrete
    • freeze rotation for x, y, z = true
    • mass=200
    • drag=15
    • angular drag = 0.05

Movement Requirements

  • move horizontal (x-axis)
  • move depth (z-axis)
  • move height (y-axis)
  • only move orthogonal (no diagonal)
  • walk at preset speed (configurable)
  • run at 2x walk (configurable)
  • crouch at 0.5x walk (configurable)
  • rotate at 90 degrees at a time
  • cannot move while rotating
  • cannot rotate while moving
  • walking, crouch walking, and running process at different speeds
  • bouncing off colliders takes time (configurable)

This meant I had two major areas of work, colliders and y-axis movement. For colliders I decided to create a “Blockers” tag and assign it to the walls. Then I added a bit of code to check for that tag in the OnCollisionEnter method.

    void OnCollisionEnter(Collision collisionInfo)
    {
        print($"Collision with: {collisionInfo.gameObject.name}");
        switch (collisionInfo.gameObject.tag.ToLower())
        {
            case string a when a.Contains("blocker"):
                isBouncing = true;
                bouncePosition = CurrentPosition;
                break;
            case string a when a.Contains("height adjusting"):
                break;
            case null:
                new System.ArgumentNullException(nameof(collisionInfo));
                break;
            default:
                break;
        }
    }

This mostly solved my collision issues, although I can still peek through walls when I run at them I no longer passed through them. This even gave me a head start on the y-axis movement as I climbed stairs with a bounce to my step, and moved forward and then fell down, just like the Coyote.

I decided I wanted multi-level scenes, like a three-story house, stairs leading to a walled off section of town, or any number of other scenarios, which meant I needed working y-axis movement. At first, I thought I needed to break out my trigonometry. Every set of stairs is a right triangle in disguise and I always knew the length of two sides and an angle, which meant I could easily compute everything else about the triangle. I assumed that if I moved one step forward I could compute the position on the triangle the character needed to stop at and then include that in the lerp code.

I played around with Rigidbody.MovePosition which, according to Unity, would give me smooth movement with the correct interpolation setting. However, in my experiments I got much better results by controlling my movement and use lerp. The code worked better, however, I still experienced jitter moving up the stairs and in various testing scenarios I would move then fall or not fall at all when trying to go down the stairs.

During my research I discovered raycasting (and boxcasts, and spherecasts, and capsulecasts, etc.). Maybe I could raycast underneath the character to compute the distance to the floor. Then I could use that to adjust the y-axis of the position the character moved towards. This eliminated my triangle trig problem and made Unity do the lerp work for me. Sure enough it worked. I included a snippet of code and some gifs of my character moving around bumping into walls (peeking through them) and climbing up and down the stairs.

private void PerformMovement(float speedModifier)
{
	if (isWalking)
	{
		isMoving = true;
		// TODO: DuringMove event should fire (this would call it every frame)
		float walkComplete = ActionCompleteFraction(walkingDuration * (1 / speedModifier));
		// TODO: check for floor under character and drop down if none
		RaycastHit hit;
		if (Physics.Raycast(CurrentPosition, transform.TransformDirection(Vector3.down), out hit))
		{
			// adjust the y position to match the point hit offset by the height of the character
			// CHECK: HalfHeight should account for player height
			nextPosition.y = hit.point.y + HalfHeight;
		}
		transform.position = Vector3.Lerp(CurrentPosition, nextPosition, walkComplete);

		if (walkComplete >= 1.0f)
		{
			isWalking = isMoving = false;
			// TODO: AfterMove event should fire
		}
	}
}

I have lots of work left on the movement, but I considered this my basic set of movement. This is enough to move forward with my gamedev. 🙂

I plan to spend some time this week creating a GitHub repo with a small ProBuilder demo scene and upload my code. I’m curious to see how people will use it and improve it. Once I created the public repo I’ll blog and tweet about it.

Keep on Questing!

Once I wrapped my head around the movement task and created a detailed set of requirements everything fell into place. I felt better about my ability to make this work. This week taught me to take a step back and spend some real time working on my Game Design Doc, and flesh out a few more coding tasks like I did with movement. I don’t need a detailed task list for everything but I should have my Overall Vision written down and then at least the next three to six steps detailed enough to make it work.

Come back next week to discover where my quest takes me next.

5 Comments »

  1. The stair movement is looking good! I’m glad you didn’t settle on the player having to hop onto each individual tread. And thank you for including the code snippets – it really helps beginners like me with the explanations of what you’ve accomplished.

    I’m unable to see the Trello board. I get this error: “Card not found.This card may be on a private board. You may be able to view it by logging in.”

    Can’t wait to see your progress in next week’s update!

    Like

  2. Fantastic start, well done – it was actually really nice to binge these first few blogs and see how far you got.
    If you’re finding it tough, just remember you’re doing amazingly well and you’re much further a long than when you started your blog, so good on you!
    I know you might not feel like you’re there yet, but do you have any ideas for your first quest you’d like to code? Are you going to start with a tutorial quest and build outward, or are you thinking that’ll come later?
    Looking forward to reading the next one!

    Liked by 1 person

    • I have given it a bit of thought, but I haven’t decided if I want to do the typical “rats in the cellar” quest or something else. Whatever the first quest turns out to be, it will have twists. I have a set of Choose Your Own Adventure and Fighting Fantasy books to use as research for branching storylines.

      I do know that I will create a separate tutorial quest and that it will be optional. I play a lot of games and I hate when I am forced directly into a tutorial with no way to turn it off.

      Like

      • That’s really neat – I’ll try to find a paper I read about five years ago regarding branching story lines as it really hammers home that the thing that ruins a games development cycle and makes it unmanageable is a lack of “merge” or “fork” points.
        I.e our protagonist can choose to be good or bad, but regardless they’ll still lose their friend near the end and they’ll still fight the main boss. You can’t account for double the possibilities if the friend is alive or not, when potentially half of your players won’t even get to see that content. Similarly you can’t code and build up to two separate boss battles. Obviously these are only examples and of course you could do this, the point I’m making is that these three decisions already have given us 8 potential branches. Without realignment of our story points, this will very quickly become unmanageable, even just one more tier of complexity.

        The paper dove into greater depth and talked about some very successful titles at the time and their effort to overcome the above. I’ll see if I can get it at some point!

        Liked by 2 people

Leave a Reply to WeirdBeardDev Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.