I’m trying to get the Y position of a 3D capsule shape in a script but if I do this code:
// player allowed to jump?
if (this.entity.y > 1) {
canJump = 0
}
if (this.entity.y = 1) {
canJump = 1
}```
//it gives me an error saying: " [[firstPersonMovement.js?id=201410242&branchId=1d3cf9c5-d080-400d-9cb9-510f7c797931:120]](https://playcanvas.com/editor/code/1269686?tabs=201410242&line=120&col=17&error=true): Uncaught TypeError: Cannot read properties of undefined (reading 'y')
TypeError: Cannot read properties of undefined (reading 'y')
at https://launch.playcanvas.com/api/assets/files/Scripts/firstPersonMovement.js?id=201410242&branchId=1d3cf9c5-d080-400d-9cb9-510f7c797931:120:17"
if checking World Position Y
if (this.entity.getPosition().y > 1) {
canJump = 0
}
if (this.entity.getPosition().y = 1) {
canJump = 1
}
if checking Local Position Y
if (this.entity.getLocalPosition().y > 1) {
canJump = 0
}
if (this.entity.getLocalPosition().y = 1) {
canJump = 1
}
This time doesn’t understand “getLocalPosition()” and “getPosition()”. I don’t understand why.
This time doesn’t understand “getLocalPosition()” and “getPosition()”. I don’t understand why.
this.entity.getLocalPosition()
in here .getLocalPosition() is a Method and it gives us that entity’s position relative to its parent simply position from it’s parent’s origin
and that position is type of pc.Vec3 it has 3 components x, y and z so we access the y component by getLocalPosition().y
this.entity.getPosition()
in here .getPosition() is a Method and it gives us that entity’s position relative to game world’s origin simply position from game world’s origin and that position is type of pc.Vec3 it has 3 components x, y and z so we access the y component by getPosition().y like same as above
I guess you need to use this.entity.getPosition().y
for more details
You should use ===
or ==
instead of =
to compare a variable.
2 Likes
What is the current issue?
It says that it doesn’t understand what “getPosition” and “getLocalPosition” mean.
Because of this, I created an 3D Box Entity and gave it a material and increased the opacity to 1 so it becomes transparent. Although this makes it a little bit inconvenient to jump, right now, that is the best solution I can think of.
Can you please share the error and your code?
1 Like
Code:
var FirstPersonMovement = pc.createScript('firstPersonMovement');
var canJump = 1
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;
}
if (app.keyboard.isPressed(pc.KEY_SPACE) && canJump == 1) {
this.entity.rigidbody.applyImpulse(0, 1.5, 0)
}
// 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);
};
// player can jump?
if (this.entity.getPosition().y > 1) {
canJump = 0
}
if (this.entity.getPosition().y === 1) {
canJump = 1
}
Error: [firstPersonMovement.js?id=201410242&branchId=1d3cf9c5-d080-400d-9cb9-510f7c797931:120]: Uncaught TypeError: Cannot read properties of undefined (reading ‘getPosition’)
TypeError: Cannot read properties of undefined (reading ‘getPosition’)
at https://launch.playcanvas.com/api/assets/files/Scripts/firstPersonMovement.js?id=201410242&branchId=1d3cf9c5-d080-400d-9cb9-510f7c797931:120:18
I did before already. They don’t give jump mechanic or explain how to make it yourself.
Line 119 till 126 of the code above is placed outside a function and cannot be read. Make sure your code is inside a function, for example the update function.
1 Like
var FirstPersonMovement = pc.createScript('firstPersonMovement');
//Dont do this
//var canJump = 1
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('jumpImpulse', {
type: 'number',
default: 250,
description: 'Adjusts the impulse of player Jump'
});
FirstPersonMovement.attributes.add('gravity', {
type: 'number',
default: 100,
description: 'Adjusts the gravity'
});
FirstPersonMovement.attributes.add('lookSpeed', {
type: 'number',
default: 0.25,
description: 'Adjusts the sensitivity of looking'
});
FirstPersonMovement.prototype.initialize = function () {
this.force = new pc.Vec3();
this.eulers = new pc.Vec3();
this.canJump = 1; // Use this or I suggest this.canJump = true;
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");
}
};
//##Read this => update code called every frame ##Read this
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;
}
if (app.keyboard.isPressed(pc.KEY_SPACE) && this.canJump == 1) {
this.entity.rigidbody.applyImpulse(0, this.jumpImpulse, 0)
}
// 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 jumpState (your code) -----------------------------
//and Dont use this because when playr in a hill or something his y will be higher that 3 so do raycast down direction get the hit point and calculate dstance to player and hit point
if (this.entity.getPosition().y > 3) {
this.canJump = 0
//I like to suggest using this
//this.canJump = false
}
if (this.entity.getPosition().y <= 3) {
this.canJump = 1
//I like to suggest using this
//this.canJump = true
}
if (this.canJump == 0) {
//We set Linear Damping to 0.99 that is almost one so playar will get dwon slowly so we add artificial gravity when playr in artificial
this.entity.rigidbody.applyForce(new pc.Vec3(0, -this.gravity, 0));
}
// 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);
};
Try this and read comments well
2 Likes