[SOLVED] Using script to move objects

Im new to basically all this. Im trying to find as many resources as possible to help me figure out how to get objects moving the way i want them to. I’ve been trying all day now and can not seem to get an object with a third person camera as a child entity to move together using wasd keys.

TIA, Joseph Settle

1 Like

do you need help with first person or just moving the object because I use this script to move the object but you may edit it to your liking:

var Movement = pc.createScript('movement');

Movement.attributes.add('speed', { type: 'number', default: 1 });
Movement.attributes.add('main', { type: 'entity' });
Movement.attributes.add('stop', { type: 'entity' });

// initialize code called once per entity
Movement.prototype.initialize = function() {

};

// update code called every frame
Movement.prototype.update = function(dt) {
    var left = this.app.keyboard.isPressed(pc.KEY_LEFT);
    var right = this.app.keyboard.isPressed(pc.KEY_RIGHT);
    var up = this.app.keyboard.isPressed(pc.KEY_UP);
    var down = this.app.keyboard.isPressed(pc.KEY_DOWN);

    if (left) {
        this.entity.translate(-this.speed * dt, 0, 0);
    }
    if (right) {
        this.entity.translate(this.speed * dt, 0, 0);
    }

    if (this.main.enabled) {
        this.entity.translate(0, 0, -this.speed * dt);
    }
        
    if (down) {
        this.entity.translate(0, 0, (this.speed * 2) * dt);
    }
    if (this.entity.getPosition().z <= this.stop.getPosition().z) {
        this.speed = 0;
    }
};

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

Heres some third person resources:https://www.google.com/search?client=firefox-b-1-d&q=playcanvas+third+person (in case you need it or want it in the future)

2 Likes

The script @Axulity_YT has posted uses translation. Translation doesn’t work well with the Physics System, so stuff like Gravity may not work. If you’d like something that doesn’t bug out the physics system, you can use this script instead:

var ExampleScript = pc.createScript('exampleScript');

/* TIPS:
    1. Make sure there's a Rigidbody component and Collision component attached to the entity.
    2. The Rigidbody must be Dynamic for this script to work without a problem.
    3. The script @Axulity posted should be used on a Static Rigidbody.
*/

//Create Attributes (Public Variable), 'mspeed'.
ExampleScript.attributes.add("mspeed", { type: 'number', default: 5, title: 'Movement Speed' });

// initialize code called once per entity
ExampleScript.prototype.initialize = function() {
    //create keyboard events, so when a key is pressed, a function can be called.
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
    this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
};

// update code called every frame
ExampleScript.prototype.update = function(dt) {
    //Event called when right arrow is pressed.
    if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
        //We apply force to the rigidbody component attached to the entity. The force is mspeed times dt (deltaTime).
        this.entity.rigidbody.applyForce(this.mspeed * dt, 0, 0);
    }
    
    //Event called when left arrow is pressed.
    if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
        //We apply force to the rigidbody component attached to the entity. The force is negative mspeed times dt (deltaTime).
        this.entity.rigidbody.applyForce(-this.mspeed * dt, 0, 0);
    }
    
    //Event called when up arrow is pressed.
    if (this.app.keyboard.isPressed(pc.KEY_UP)) {
        //We apply force to the rigidbody component attached to the entity. The force is mspeed times dt (deltaTime).
        this.entity.rigidbody.applyForce(0, 0, this.mspeed * dt);
    }
    
    //Event called when down arrow is pressed.
    if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
        //We apply force to the rigidbody component attached to the entity. The force is negative mspeed times dt (deltaTime).
        this.entity.rigidbody.applyForce(0, 0, -this.mspeed * dt);
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// ExampleScript.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

@Axulity_YT’s script is bet used on a Static Rigidbody, so that Gravity won’t affect the game.

2 Likes

4 posts were merged into an existing topic: Problem with a script animation