[solved] Setting a volocity


#1

im trying to have a set velocity for whenever the player moves

and ive tried these

this.entity.rigidbody.linearVelocity.set(-500,null,0);
this.entity.rigidbody.linearVelocity.x=-500

#2

Are you sure that you need the .set? I’d try it without that and I also wouldn’t use “null” for a test.

Anyway, these are just newbie suggestions. There’s a very good chance I’m wrong. But I haven’t seen .set used in any of the rigidbody examples I’ve seen. I also haven’t seen the .x notation used.


#3

One the slight oddities of the PlayCanvas engine is that sometimes you have to assign the object for it to ‘register’.

You can see this in the setter of the linearVelocity property: https://github.com/playcanvas/engine/blob/master/src/framework/components/rigid-body/component.js#L92

The general idiom is to do the following:

var v = this.entity.rigidbody.linearVelocity;
v.set(-500, 0, 0);
this.entity.rigidbody.linearVelocity = v;

Or have a temp pc.Vec3 and use that instead.


#6

its still not working


#7

Can you show me how to use vec.3 (mabe Vec.2 because im making a 2d game)?


#8

https://playcanvas.com/editor/scene/733034


#9

I cant see how you are trying to set the velocity in your project?


#11

look through again, i placed it back in


#12

here is the code if you cant find it

 var vol = this.entity.rigidbody.linearVelocity;
    var vol2 = this.entity.rigidbody.linearVelocity;
    var vol3 = this.entity.rigidbody.linearVelocity;
    vol.set(200,null,0);
    vol2.set(-200,null,0);
    vol3.set(0,null,0);
    //l&r movement
    if(this.app.keyboard.isPressed(pc.KEY_LEFT)){
        this.entity.sprite.flipX=true;
        this.entity.rigidbody.linearVelocity = vol;

    }
    if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
        this.entity.sprite.flipX=false;
        this.entity.rigidbody.linearVelocity = vol2;
    }
    //stopping motions
     if(this.app.keyboard.wasReleased(pc.KEY_LEFT)){
        this.entity.rigidbody.linearVelocity = vol3;

    }
    if(this.app.keyboard.wasReleased(pc.KEY_RIGHT)){
       
        this.entity.rigidbody.linearVelocity = vol3;
    }

#13
    var vol = this.entity.rigidbody.linearVelocity;
    var vol2 = this.entity.rigidbody.linearVelocity;
    var vol3 = this.entity.rigidbody.linearVelocity;
    vol.set(200,null,0);
    vol2.set(-200,null,0);
    vol3.set(0,null,0);

A few issues here.

null shouldn’t be used. What I’m guessing is that you didn’t want to modify the existing y velocity.

    vol.set(200,vol.y,0);
    vol2.set(-200,vol2.y,0);
    vol3.set(0,vol3.y,0);

However, as linearVelocity is a pc.Vec3 which is a JS object, it is return a reference to the velocity so vol, vol2 and vol3 are all referencing the same pc.Vec3 object.

This means that by the end of this, the values of vol, vol2 and vol3 are all the same because they are the same object.

This is my fixed version:

    //l&r movement
    var vol = this.entity.rigidbody.linearVelocity;
    //l&r movement
    if(this.app.keyboard.isPressed(pc.KEY_LEFT)){
        this.entity.sprite.flipX=true;
        vol.x = -2;
        this.entity.rigidbody.linearVelocity = vol;

    }
    if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
        this.entity.sprite.flipX=false;
        vol.x = 2;
        this.entity.rigidbody.linearVelocity = vol;
    }
    //stopping motions
     if(this.app.keyboard.wasReleased(pc.KEY_LEFT) || this.app.keyboard.wasReleased(pc.KEY_RIGHT)){
        vol.x = 0;
        this.entity.rigidbody.linearVelocity = vol;

    }

https://playcanvas.com/editor/scene/733226


#14

PRASE THE LORD!!! thank you!


#15

one last thing. I’m trying to set a timer to delete the shot clones but the cloning nor the shot’s script won’t run

setTimeout(clone.destroy(),60000);


#16

Can you post the full function around this code please?


#17

it is part of the shotmove script

if(this.enabled===true){
        if(this.isflip){
            this.entity.translate(-15*dt,0,0);
        } else{
            this.entity.translate(15*dt,0,0);
        }
        setTimeout(this.entity.destroy(),60000);
    }


#18

setTimout is expecting a function object (I’m a little surprised that the code doesn’t throw an error here). So the code should look something like this using a closure:

setTimeout(function() { this.entity.destroy(); },60000);

However, as this is a closure, the this reference is not pointing to the pc.Script instant that we are calling the setTimeout from. We need to create a variable to capture in the closure.

var self = this;
setTimeout(function() { self.entity.destroy(); },60000);

Looking at the surrounding code, it looks like you are calling the code in the update function which means it is creating a new timed callback each frame. You only want to call setTimeout once.

See this thread for someone who had a similar issue: [SOLVED] Delay for the shot


#19

Well i decided to take a look at this (Although i ask the most questions) And see if i can find something :slight_smile: Well i found this:

    this.isflip = this.player.sprite.flipX;

it says flipX but when you look further through the code he does:

if(this.isflip)

You have to keep the variable the same or if he is using the X axis when he says flipX shouldnt it be flip.x? (I get that isflip is the caller) But instead of flipX its flip.x calls on the x value so when he does:

 if(this.isflip){
        this.entity.translate(-10*dt,0,0);
    } else{
        this.entity.translate(10*dt,0,0);
    }

of course it should do what he wanted it to do right?


#20

that is not what this is about, it was about using setTimer(); to destroy a entity


#21

I decided to just go with this

this.delete+=dt;
        if(this.delete>=100){
            this.entity.destroy();
        }

We can now leave this post