Better First Person Movement?

HI, I am trying to find a better First Person Movement script, but can’t find what I want. I tried to make my own, based on the one made by Playcanvas, but ran into some problems.

What I am looking for:

  • Physics friendly (The premade one is, but it is constantly normalizing y movement so falling is super slow, and I can’t make it friendly to falling objects.)
  • Rotates the object the the camera is attached to (The premade one only rotates the camera. I tried to make it rotate the “player” but it only rotates the “player” until you move (it also doesn’t let you look around until you move), it also makes movement weird.)

The FPM script
The project

Is this not possible?

Hi @PitchBlackNights and welcome!

I opened a topic for this myself, maybe it can help you.

I don’t quite understand what you mean by this. You don’t see the player in first person, do you? And according to the tutorial below you can also rotate the camera when you are not moving.

https://developer.playcanvas.com/en/tutorials/first-person-movement/

Sorry for being confusing, what I mean is:
I don’t see the player (I don’t want to), but I want the “player” to rotate when I rotate the camera. I want this because of shadows. I want to see the “players” shadow rotate when I rotate.

What happens if you apply the same rotation to the parent entity of your camera? (I didn’t see your project yet, because you shared the same code editor link twice).

https://playcanvas.com/editor/scene/1319246
here, updated the link

On reflection, I don’t think this is a good idea. I wouldn’t know off the top of my head and should give it a try. Perhaps someone will know the answer.

I guess I deleted the code that “rotates” the parent entity, so I am going to have to remake the code, but I think I rotated the parent entity using the same Euler angles that were being applied to the camera, but I didn’t apply rotation directly from the camera rotation.

Nvm, I didn’t delete the code, here it is:

FirstPersonMovement.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;

        this.entity.setEulerAngles(0, this.eulers.x, 0); // I added this line
    }            
};

This looks like it is actually rotating the parent entity, but it’s not?

That’s smart. It should do at least something. You can try to add console.log(this.entity.getEulerAngles().y); and check it in the console of your browser.

Ok, so 3 things:

  1. Rotation is great before I first move
  2. It is trying to rotate the entity while I move, but the rotation is constantly being reset (Idk why or where) and is messing with movement (the movement is stuttery when I look in a certain direction)
  3. When I stop moving, after about 2 seconds, the rotation works perfectly

I don’t know what todo to fix this

I think that’s because you use a dynamic rigidbody. Please try to use teleport() instead of setEulerAngles().

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#teleport

Ok, I did this:

FirstPersonMovement.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
        var position = this.entity.getPosition()   // added this
        this.entity.rigidbody.teleport(position, 0, this.eulers.x, 0)   // added this
    }            
};

But now there is no rotation, and the stuttering seems worse. Should I make the rotation a vector?

If you use a vector for position you also have to use a vector for rotation.

IT WORKS! Well… the movement is still a little stuttery, but that just a minor inconvenience!

The movement of the position or the rotation?

the movement of position

Maybe you can add a postUpdate function and try to swap something to this function.

I’ve checked your project on my laptop, but I don’t see any stuttering in movement at all. The problem is the update of the shadows. The result is the same in the original tutorial project (when you make the shadows visible in that project). A shadow expert might be able to tell you how to improve this. I suggest to open a new topic for this.