Third person movement (like Nex Machina)

I’m trying to come up with a movement controller similar to how Nex Machina works.

Basically I’d like the player to move with WASD and aim with the mouse. (Next up will be with a controller and the twin stick for mobile, but that’s going to kick in after I’m done with the keyboard and mouse)

Moving the player is straightforward, but aiming with the mouse seems to be a bit more complicated. Is there anyone who already did something similar and that can either point me to an algorithm?

I thought I’d just project the mouse position on the floor mesh and get that intersection point and use LookAt to actually look at it, eventually ignoring the rotation around the X axis, but maybe there’s a better way to achieve this. Thanks in advance!

For those that don’t know the game I’m referring to, here’s a video showing some gameplay:

There’s Deftly.

@LaneFox recently posted some examples of verticality in aiming, too.

1 Like

If you have no verticality you can use a plane.

       protected Vector3 GetLookGoalFromCursor()
        {
            // Wizardry.
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
            Plane p = new Plane(Vector3.up, transform.position);
            float dist;
            p.Raycast(r, out dist);
            Vector3 mouseWorldPos = r.GetPoint(dist);      
            Vector3 goalVectorDirection = mouseWorldPos - transform.position;
            return goalVectorDirection;
        }

Otherwise you have to setup a raycast from the screen, by the mouse position, filtering ‘aimable’ layers (eg, environment/floor) then do some fairly non-trivial work to make the avatar aim correctly.

Deftly (current version) doesn’t support aiming verticality, but there is a 2.0 version (in progress) which will support an absurd amount of new things including vertical aiming. For non-verticality, you only need the Y rotation. XZ is always zero rotation. Depending on how you aim vert and handle locomotion, you probably never need the XZ for the avatar rotation.

Thanks to both of you @TonyLi and @LaneFox !

For the time being I went for a quick solution:

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hit = new RaycastHit[1];
if (Physics.RaycastNonAlloc(ray, hit, 100) > 0) {
  Debug.DrawLine (transform.position, hit[0].point);
  transform.LookAt(hit[0].point, Vector3.up);
}

It’s nothing fancy but it gets the work done with my current simple setup.

I have not yet though about locomotion. I’m pretty sure I will only need to move on the XZ plane and aiming isn’t going to need to aim up and down like with stairs and the like. Everything’s going to move on a plane or float above it at a fixed distance from the floor so there’s no need for verticality (this should make things simpler).

As for the movement itself I’ll have to look at Unity’s builtin locomotion. I suppose there’s enough in there for what I need.

@LaneFox I checked the videos of Deftly and I’ve seen that you’re using a nice test character in there. Is that model+animation a free asset that I can use as well while prototyping?

Cheers!

The 2.0 release will include a full root motion animation set with verticality support and a PBR textured, rigged, ready to go ‘mannequin robot’ character very similar/equivalent to the one in the video. Targeting October for a release, atm.

2 Likes