Oh and i must add if you have not looked at the original project example, The character is created through a script and is not a rigid body or anything like that, So i cannot use attributes.
Sorry, I don’t know what you mean and what you are asking of the users here on the forum.
I am asking the users to help me add a jump feature for this project example I found. I am asking that someone could help because I am new to this. The previous message i sent stated that the character is not rigid body.
-Cheers
As far I can see, the script you use is for a character with a rigidbody component. When you use that script on a character without a rigidbody component you will get an error.
You can also use attributes without using a rigidbody component.
I apologize, I am wrong. Like I said, I am extremely new to all of this.
-Cheers
(i still need help 3 days left)
I understand you need help, but you need to apply the help yourself. You can try to search on the forum for some other topics about jumping, because I think there are a lot.
I have, and that’s when I resorted to making a forum post. The problem with most of the stuff I tried is that they are using stuff that does not work in the script I’m using, like when the character moves it applies a force, Thats what i could not find when searching the web and the forum.
-Cheers
You basically only need to apply an upwards force when the spacebar was pressed.
// update function
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
this.entity.rigidbody.applyImpulse(0, 5, 0);
}
For my example project I also use a raycast to detect if the character is on the ground.
all right, I know I’m a pain in Your butt at this point but when I add the script in am i supposed to modify it or is it just a copy paste thing?
I think you can add this part of code without any changes to the update function of your PlayerMovement script. Maybe you need to adjust the jump force (currently 5) to achieve the desired result.
It seems that I copied code from another topic with a mistake. I will correct this. Below the corrected code.
// update function
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
this.entity.rigidbody.applyImpulse(0, 5, 0);
}
OMG NEVERMIND. i changed a value and it works!!!
Great! I’m happy you have a good result now.
Now I am just looking through your jumping example and trying to figure out how to make it fall faster to the ground and how to have it only jump once.
lol
Yeah now i need help with only having it jump once, is there a simple way to do this, I’m looking at the jump example and it does not make too much scence to me.
In my example I use a downwards raycast from the character, to detect if there is ground below the character. When the character is already jumping, the raycast doesn’t detect the ground anymore, because I have given the raycast a limited length. (A raycast is basically an invisible line to detect other entities).
Hello! so I’ve tried added some code to have the player only jump twice when space key is pressed (I want a double jump) and nothing has worked. Here is the code and where I’m at right now (by the way I’m not getting any errors when running the game)
var PlayerMovement = pc.createScript('playerMovement');
PlayerMovement.attributes.add('speed', { type: 'number', default: 38888 });
PlayerMovement.attributes.add('length', {
title: 'Sensor length',
type: 'number',
default: 1
});
PlayerMovement.prototype.initialize = function() {
};
PlayerMovement.prototype.initialize = function () {
var app = this.app;
var camera = app.root.findByName('Camera');
this.cameraScript = camera.script.cameraMovement;
};
PlayerMovement.prototype.update = function (dt) {
var app = this.app;
var forward = this.entity.forward;
var right = this.entity.right;
var x = 0;
var z = 0;
if (app.keyboard.isPressed(pc.KEY_A)) {
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.wasPressed(pc.KEY_SPACE)) {
this.entity.rigidbody.applyImpulse(0, 2222, 0);
}
PlayerMovement.prototype.jump = function() {
var start = this.entity.getPosition();
var end = new pc.Vec3(start.x, start.y - this.length, start.z);
var ground = this.app.systems.rigidbody.raycastFirst(start, end);
if (ground) {
this.entity.rigidbody.applyImpulse(0, this.force, 0);
this.jump();
}
};
PlayerMovement.prototype.jump = function() {
var start = this.entity.getPosition();
var end = new pc.Vec3(start.x, start.y - this.length, start.z);
var ground = this.app.systems.rigidbody.raycastFirst(start, end);
if (ground) {
this.entity.rigidbody.applyImpulse(0, this.force, 0);
}
};
if (x !== 0 || z !== 0) {
var pos = new pc.Vec3(x * dt, 0, z * dt);
pos.normalize().scale(this.speed);
pos.add(this.entity.getPosition());
var targetY = this.cameraScript.eulers.x + 180;
var rot = new pc.Vec3(0, targetY, 0);
this.entity.rigidbody.teleport(pos, rot);
}
};
Making it more difficult than it already is for you?
I think if you replace line 54 with this.jump();
and on line 68 this.force
with your jump force, it should be good for a normal jump. Line 67 can be removed.
I’m so confused, I mean i understand it but I don’t at the same time
var PlayerMovement = pc.createScript('playerMovement');
PlayerMovement.attributes.add('speed', { type: 'number', default: 38888 });
PlayerMovement.attributes.add('length', {
title: 'Sensor length',
type: 'number',
default: 1
});
PlayerMovement.prototype.initialize = function() {
};
PlayerMovement.prototype.initialize = function () {
var app = this.app;
var camera = app.root.findByName('Camera');
this.cameraScript = camera.script.cameraMovement;
};
PlayerMovement.prototype.update = function (dt) {
var app = this.app;
var forward = this.entity.forward;
var right = this.entity.right;
var x = 0;
var z = 0;
if (app.keyboard.isPressed(pc.KEY_A)) {
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.wasPressed(pc.KEY_SPACE)) {
this.entity.rigidbody.applyImpulse(0, 2222, 0);
this.jump();
}
PlayerMovement.prototype.jump = function() {
var start = this.entity.getPosition();
var end = new pc.Vec3(start.x, start.y - this.length, start.z);
var ground = this.app.systems.rigidbody.raycastFirst(start, end);
this.force
PlayerMovement.prototype.jump = function() {
var start = this.entity.getPosition();
var end = new pc.Vec3(start.x, start.y - this.length, start.z);
var ground = this.app.systems.rigidbody.raycastFirst(start, end);
if (ground) {
this.entity.rigidbody.applyImpulse(0, this.force, 0);
}
};
if (x !== 0 || z !== 0) {
var pos = new pc.Vec3(x * dt, 0, z * dt);
pos.normalize().scale(this.speed);
pos.add(this.entity.getPosition());
var targetY = this.cameraScript.eulers.x + 180;
var rot = new pc.Vec3(0, targetY, 0);
this.entity.rigidbody.teleport(pos, rot);
}
};