I am trying to add a car that moves back and forth but it only moves once and just keeps going and I can’t find out the problem here is the script:
var Carmove = pc.createScript('carmove');
// initialize code called once per entity
Carmove.prototype.initialize = function() {
this.checkpoint1 = this.app.root.findByName("Checkpoint1");
this.checkpoint2 = this.app.root.findByName("Checkpoint2");
};
// update code called every frame
Carmove.prototype.update = function(dt) {
if (this.entity.getPosition().distance(this.checkpoint1.getPosition()) > 2.5) {
this.entity.translateLocal(0,0,-0.05);
}
if (this.entity.getPosition().distance(this.checkpoint2.getPosition()) > 2.5) {
this.entity.translateLocal(0,0,-0.05);
}
};
// swap method called for script hot-reloading
// inherit your script state here
// Carmove.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/`
Hi @00ferman2,
Where are the Checkpoint entities placed? And what is your car’s initial position?
If you can provide a sample project url, it will help debug this.
What i can see is that you don’t change the rotation of the car to his new checkpoint so the car keeps moving in the same direction.
i have changed it but now it doesn’t move at all - https://playcanvas.com/editor/scene/894558
This is working:
var Carmove = pc.createScript('carmove');
// initialize code called once per entity
Carmove.prototype.initialize = function() {
this.checkpoint1 = this.app.root.findByName("CheckpointsCar").findByName("Checkpoint1");
this.checkpoint2 = this.app.root.findByName("CheckpointsCar").findByName("Checkpoint2");
};
// update code called every frame
Carmove.prototype.update = function(dt) {
if (this.entity.getPosition().distance(this.checkpoint1.getPosition()) > 2.5) {
this.entity.translateLocal(0,0,-7*dt);
}
else {
this.entity.lookAt(this.checkpoint2.getPosition().x, this.entity.getPosition().y, this.checkpoint2.getPosition().z);
}
if (this.entity.getPosition().distance(this.checkpoint2.getPosition()) > 2.5) {
this.entity.translateLocal(0,0,-7*dt);
}
else {
this.entity.lookAt(this.checkpoint1.getPosition().x, this.entity.getPosition().y, this.checkpoint1.getPosition().z);
}
};
1 Like