Hello has anybody tried movement of object using waypoints on the map? I want my player to hold up arrow key and the character would automatically follows the waypoints in order, i mean something like creating a new array where all waypoints would be stored in order and once my player get on the first waypoint position it would go to the next second waypoint position and so on until he reaches the finish line, here i have a screenshot of map and numerical order of waypoints.
.
@smokys Ok an possible idea is to get the distance to the certain point and if the distance is less than 2 it deletes the point and goes to the other
Failure...
myscript.prototype.initialize = function(){
this.Point = 1;
};
myscript.prototype.update = function(){
this.entity.lookAt(this.app.root.findByName('Point' + this.Point.toString()).getLocalPosition());
this.Point = pc.math.clamp(this.Point,0,10/* it doesnt exactly have to be ten you can choose what number*/);
if(this.app.keyboard.isPressed(pc.KEY_UP){
this.entity.translateLocal(0,0,1);
}
var point = this.app.root.findByName('Point' + this.Point.toString()).getLocalPosition());
var playerPosi = this.entity.getLocalPosition();
var distance = point.distance(playerPosi);
if(distance < 2){
this.app.root.findByName('Point' + this.Point.toString()).destroy();
this.Point++;
}
// you would need to make a point for every number you want. in the hierarchy
};
Hi @smokys,
Check this example project, it contains a script to easily build a list of points and have an entity on the path:
https://developer.playcanvas.com/en/tutorials/camera-following-a-path/
I would go with the curve method, as Leonidas pointed. You can use a curve with linear steps to have sharp truns, instead of a smoothed one, like in camera example.
Thats a strange
EDIT: that one is fixed
had to remove getLocalPosition because it showed error
EDIT2: when i try to show distance in console it says undefined, also i heard that lookat function is reversed, thats why my player doesnt face the current following point but 180degree so on the other side.
EDIT3: distance and increasing point value is working
lookat is facing to bad side, also lookat function is being proccesed 1 time in in 3 seconds, when i push up arrow it will still go forward off the edge
oops made an error
nvm i fixed it so there is no error, but its not working properly
I cant figure out why look at function is fired like every 3 seconds, when i press up arrow then lookat is not working
I remade the waypoint system but sadly without smooth lookAt / turning.@smokys You shouldn’t get an error every time it reaches the last waypoint.
var Waypoint = pc.createScript('waypoint');
Waypoint.attributes.add('Waypoint',{type:'entity'});
// initialize code called once per entity
Waypoint.prototype.initialize = function() {
this.Point = 0;
var points = this.Waypoint.children;
var object = this.entity;
var pointpos = points[this.Point].getLocalPosition();
this.MaximumPoints = 0;
this.MinimumPoints = 0;
this.finished = false;
};
// update code called every frame
Waypoint.prototype.update = function(dt) {
var points = this.Waypoint.children;
var object = this.entity;
var pointpos = points[this.Point].getLocalPosition();
var distance = points[this.Point].getLocalPosition().distance(this.entity.getLocalPosition());
this.MaximumPoints = points.length;
if(this.finished === false){
object.lookAt(pointpos);
if(this.app.keyboard.isPressed(pc.KEY_UP)){
object.translateLocal(0,0,-0.05);
}
}
if(distance < 0.5){
if(this.Point != this.MaximumPoints-1){
this.Point++;
}else{
this.finished = true;
}
}
};