[SOLVED] How could I get the time at the beginning of current frame in PlayCanvas?

like Time.time in unity for the same.

Hi @yash_mehrotra,

You can use the following method:

pc.now(); // in ms

I wrote this code to make an entity go up down repetitively in unity using c#
this.position = some_pos + Vector3.up * Mathf.Abs(Mathf.Sin(Time.time * 5));

what am I doing wrong in play canvas for the same?

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

    this.pos = new pc.Vec3();
};

// update code called every frame
Demo.prototype.update = function(dt) {

    this.pos = pc.Vec3.ZERO + pc.Vec3.UP * Math.abs(Math.sin(pc.now() * 5));
    this.entity.setLocalPosition(this.pos);
    //console.log(pc.now());
};
1 Like

Got it working. Thanks anyway.

// update code called every frame
Demo.prototype.update = function(dt) {
    this.timer += dt;
    this.pos.add2(this.entity.getLocalPosition(), pc.Vec3.UP);
    this.pos.y = Math.abs(Math.sin(this.timer * 5));
    this.entity.setLocalPosition(this.pos);
    //console.log(pc.now());
};
1 Like

Yes, that’s a JavaScript limitation, those kind of math operation aren’t supported on arbitrary types (=no operation overloading).

Thats not quite correct as you are using the Y position of the object when you multiple by sin instead of just world up

Try:

// update code called every frame
Demo.prototype.update = function(dt) {
    this.timer += dt;
    this.pos.set(0, Math.abs(Math.sin(this.timer * 5)), 0);
    this.pos.add(this.entity.getLocalPosition());
    
    this.entity.setLocalPosition(this.pos);
    //console.log(pc.now());
};
1 Like

Naah, this code does not achieve the desired result. It just moves the ball up.