Monkey Mayhem – Dev Log 03

By October 2, 2018 Dev Blog

Welcome to Development Log 3. Last time I managed to get the character to attach and detach (see below). This time I’m going to make the controls a bit better and add in another player to allow for some local multiplayer action!

Time for more Forces

The way we are currently rotating around the branch feels very clunky. So instead of using a the hinges motor, I’m going to use Torque. This will also simplify some of the hinge creation when attaching to a branch. So the way i’m going to rotate the player is with the following code:

rigid.AddTorque(Vector3.forward * Input.GetAxis("Horizontal" + playerSuffix) *  force);

The variable playerSuffix, allows for multiple players to use the same script. The force variable is public to allow me to adjust the feel within the editor.

Seeing as we no longer need a motor on our hinge, I removed that code from the SetUpHingeJoint function.

 

I also don’t like the way the current forward calculation is done. So i’m going to do it a more complex way.

Firstly, I’ll get the current Z axis angle of the player and turn it into a radian. Then I’ll get the sin and cos of this angle. Next i’ll do the following to get my forward vector in relation to the x and y axis’.

currentForward = new Vector3(
            fakeForward.x * cos - fakeForward.y * sin,
            fakeForward.x * sin + fakeForward.y * cos,
            0.0f
            );

This ends up giving me a much better forward then before.

 

Additionaly, we decided that we wanted the player to be able to wrap around the screen. The way i implemented this was by having a public rect to set the screen size and then use the following function to change the position.

void KeepOnScreen()
{
    pos = transform.position;
    //Check if we are outside the bounds
    if(!screenLimits.Contains(pos))
    {
        if (pos.x < screenLimits.xMin)
            pos.x = screenLimits.xMax;
        else if (pos.x > screenLimits.xMax)
            pos.x = screenLimits.xMin;
        if (pos.y < screenLimits.yMin)
            pos.y = screenLimits.yMax;
        else if (pos.y > screenLimits.yMax)
            pos.y = screenLimits.yMin;
    }
    transform.position = pos;
    //Keep our y velocity at a resonable speed
    if (rigid.velocity.y < maxYVelocity)
        rigid.velocity = new Vector3(rigid.velocity.x, maxYVelocity, rigid.velocity.z);
}

Using a rect allowed me to use helper variables like xMin and xMax. You may also notice that I added in an extra piece of code here to limit our velocity. This is because the player was constantly wrapping, which in turn continued to increase the y velocity.

The result of all the code I wrote, can be seen below with multiple players.

That’s all for today, Next on my list is to add an increment to the jump force depending on how long you hold the jump button.

 

See you next time,

Ethan

Leave a Reply