Devlog #3

NPCs: Your Floppy Friends and You

A superhero isn’t a superhero without bystanders to rescue and/or terrorise. I’ve already decided that for the game to work it needs a city that reacts to the player’s presence. That means people, and people means ragdolls.

They’re just resting

Since I already had some code to make ragdolls and animations work well together, I just copy-pasted the player character, removed all the special superhero code I added and stuck in the AI Character Control script from the Unity Standard Assets (it’s a pretty simple job to just tell the navigator to move to a given point, but there’s no point reinventing the wheel). Then I swapped out the superhero model (well, at the moment it’s a cop) for a more everyday model and bam! Our city has humans in it!

    private void Update()
    {
        if (target != null)
            agent.SetDestination(target.position);

        if (agent.remainingDistance > agent.stoppingDistance)
            character.Move(agent.desiredVelocity, false, false);
        else
            character.Move(Vector3.zero, false, false);
    }

    public void SetTarget(Transform target)
    {
        this.target = target;
    }

That’s basically the whole AI Character Control script. Not exactly skynet is it mate

Remember the impactThreshold value from the last post? That’s basically how ‘tough’ the character is, and how hard of a hit they’ll take before they give up and go floppy. It makes sense for that to be much higher for the superhero.

Image
Not too high though

Of course, this doesn’t work consistently because I’m calculating the impact force all wrong. Twenty minutes of googling later and I find a forum thread where someone’s kindly explained the maths for me, so I yet again manage to sidestep learning anything.

    var collisionForce = Vector3.Dot(contact.normal, other.relativeVelocity);

I barely understand what this does, but as far as I can tell it multiplies two things: (A) a direction coming perpendicular off the surface of the point of contact, and (B) the difference in speed between the two colliding bodies. I wasn’t good at maths in school so I just wrote a line to list the force of each collision then I ran around bumping into things (just like at school). The numbers got bigger the faster I was going so I figure it’s probably working.

Now we just tweak the impactThreshold for the civilians and…

Maybe hit those guys too hard

Perfect. Ideally we want the player to avoid flying at mach 10 and turning innocent bystanders into spaghetti, but the option should be there.

Now for some stress testing.

NPCs tend to ‘cluster’ when stressed or anxious

Perfect. There’ll be a few weeks of fixing bugs where civilians randomly fly upwards at 200mph and get stuck in walls vibrating like lovehoney factory rejects, but on the whole I’d say this is progress!

Leave a Reply