i have this set of codes here, to move an entity model…
i set it so that if time <= 0 seconds, this.upDone = true
but when i run it, with the console.log, the time value never drops below 1.9xxxx, why is that so?
this.upDone = false;
if (time <= 0)
{
this.upDone = true;
time = 2;
}
if (this.upDone === false)
{
time -= dt;
if (this.rightMax === true || this.leftMax === true)
{
//go up
drone.translateLocal(0, 0, -1 * dt);
}
}
Hello @bestdenki7 ! Do you use this code in the update
function?
saif
October 9, 2020, 5:56pm
3
There can be two issues:
The time variable may be a local one instead of global and for that reason it’s value is not coming to zero.
If first one is not the case then you can use .toFixed() function to fix the issue. Here is a given example:
var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
Either fix the time value with 1 or 2 and it will be fine.
Where do you put the console log?
so would it work if i add it as
Input.attributes.add(‘timer’, {type: ‘number’});//
and access this.timer?
the one im using right now i just have it set var time = this.timer;
1 Like
Where is the time variable being declared? Can you post the whole function please?
saif
October 10, 2020, 9:42am
8
I guess the issue is due to local variable or you are iniliazing variable with same value again and again. You should better initialize the time variable in the initialize function by this format:
this.time = this.timer;
Then use this this.time variable where you want and hopefully it will resolve the issue.
var FlightPath = pc.createScript('flightPath');
// initialize code called once per entity
FlightPath.prototype.initialize = function() {
this.timer = 2;
this.leftMax = false;
this.rightMax = true;
this.upDone = true;
};
// update code called every frame
FlightPath.prototype.update = function(dt) {
var drone = this.entity;
var dronePos = drone.getPosition();
var time = this.timer;
if (dronePos.x <= -12.5)
{
this.rightMax = false;
this.leftMax = true;
this.upDone = false;
}
else if (dronePos.x >= 12.5)
{
this.leftMax = false;
this.rightMax = true;
this.upDone = false;
}
if (time <= 0)
{
this.upDone = true;
time = 2;
}
if (this.upDone === true)
{
if (this.leftMax === true)
{
//go right
drone.translateLocal(10 * dt, 0, 0);
}
else if (this.rightMax === true)
{
// go left
drone.translateLocal(-10 * dt, 0, 0);
}
}
if (this.upDone === false)
{
time -= dt;
if (this.rightMax === true || this.leftMax === true)
{
//go up
drone.translateLocal(0, 0, -1 * dt);
console.log('countdown: ' + time);
}
}
/* //debuggings
console.log('leftMax: ' + this.leftMax);
console.log('rightMax: ' + this.rightMax);
console.log('upDone: ' + this.upDone);
*/
};
it is poorly coded but
this.upDone will be set to false.
and timer will start to countdown 2seconds, and if it hits 0seconds, updone will be set to true.
yaustar
October 11, 2020, 7:02am
11
This line is copying the vaule of this.timer
so this.timer` never changes value
Either change where you use time
to this.timer
or at the end of the update function, assign the value of time
back to this.timer
.
1 Like
oh! is it fine if i set var time = this.timer in initialize instead?
yaustar
October 11, 2020, 7:06am
13
No, if you do that then the variable time
is scoped in the initialise function and cannot be used in the update function.
1 Like
actually i could just scrap var time and just access this.timer thats initialized in the initialize() right
yaustar
October 11, 2020, 7:09am
15
Yes, this is what I meant by saying
got it! thank you so much for the help!!
you should put [SOLVED] in the title if you don’t need help anymore
sorry i couldnt figure out how or where to edit the topic