Can someone help me in first person view? The mouse is inverted

the keyboard control was also inverted, I fixed that, I don’t how to fix the mouse inversion
to be more detailed it is inverted y
here is the script I used-

var FirstPersonMovement = pc.createScript(‘firstPersonMovement’);

FirstPersonMovement.attributes.add(‘camera’, {
type: ‘entity’,
description: ‘Optional, assign a camera entity, otherwise one is created’
});

FirstPersonMovement.attributes.add(‘power’, {
type: ‘number’,
default: 2500,
description: ‘Adjusts the speed of player movement’
});

FirstPersonMovement.attributes.add(‘lookSpeed’, {
type: ‘number’,
default: 0.25,
description: ‘Adjusts the sensitivity of looking’
});

// initialize code called once per entity
FirstPersonMovement.prototype.initialize = function() {
this.force = new pc.Vec3();
this.eulers = new pc.Vec3();

var app = this.app;

// Listen for mouse move events
app.mouse.on("mousemove", this._onMouseMove, this);

// when the mouse is clicked hide the cursor
app.mouse.on("mousedown", function () {
    app.mouse.enablePointerLock();
}, this);            

// Check for required components
if (!this.entity.collision) {
    console.error("First Person Movement script needs to have a 'collision' component");
}

if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
    console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
}

};

// update code called every frame
FirstPersonMovement.prototype.update = function(dt) {
// If a camera isn’t assigned from the Editor, create one
if (!this.camera) {
this._createCamera();
}

var force = this.force;
var app = this.app;

// Get camera directions to determine movement directions
var forward = this.camera.forward;
var right = this.camera.right;
   

// movement
var x = 0;
var z = 0;

// Use W-A-S-D keys to move player
// Check for key presses
if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
    x -= right.x;
    z -= right.z;
}

if (app.keyboard.isPressed(pc.KEY_D)) {
    x += right.x;
    z += right.z;
}

if (app.keyboard.isPressed(pc.KEY_W)) {
    x += forward.x;
    z += forward.z;
}

if (app.keyboard.isPressed(pc.KEY_S)) {
    x -= forward.x;
    z -= forward.z;
}

// use direction from keypresses to apply a force to the character
if (x !== 0 && z !== 0) {
    force.set(x, 0, z).normalize().scale(this.power);
    this.entity.rigidbody.applyForce(force);
}

// update camera angle from mouse events
this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);

};

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;
}
};

FirstPersonMovement.prototype._createCamera = function () {
// If user hasn’t assigned a camera, create a new one
this.camera = new pc.Entity();
this.camera.setName(“First Person Camera”);
this.camera.addComponent(“camera”);
this.entity.addChild(this.camera);
this.camera.translateLocal(0, 0.5, 0);
};

Hi @GUNKATI and welcome,

Without knowing your scene structure, one way to fix that is by changing the direction of the look rotation here, I’ve changed it to += instead of -=. Try this yourself:

this.eulers.x += this.lookSpeed * e.dx;
this.eulers.y -= this.lookSpeed * e.dy;
1 Like

Thanks for the help, but I need help with jumping in my other game - https://playcanvas.com/project/756380/overview/eparkour
Can you please fork this project and add a script which allows me to jump @Leonidas

I’m having trouble to import the jump script in the game .
That’s why I need you to import it in the forked version/

Hi @GUNKATI,

For jumping add something like this in your movement.js script:

if( this.app.keyboard.wasPressed(pc.KEY_SPACE) ){
   this.entity.rigidbody.applyImpulse(0, 1000, 0);
}
1 Like

@Leonidas Thanks for the help, but the problem is, I can jump infinitely in the air.

Hi @GUNKATI, you need to detect when the entity is on the ground and allow jumping only then.

You can use collision detection, or raycasts or a simple height check to detect that.

@Leonidas Sorry if I am asking too much, but can you fork this project - https://playcanvas.com/project/756380/overview/eparkour
and add a working gun into it, it would be amazing if you did it.

Given the number of requests for having a working gun example (and a Sonic one as well), it may be a good idea to do that. @GUNKATI, I am out of time currently, but I am tackle this in the future.

@Leonidas You can take your time to finish those projects before mine :slight_smile:

Nice work because you respond to these problems so quickly, like within a day