Hi I’m trying to move an entity to the position of a another one I know it must be in the update section using and I giving the position and all but I just don’t get how to move it instead of teleport it
My code
var BtnMenu = pc.createScript('btnMenu');
BtnMenu.attributes.add('id', {
type: 'number'
});
BtnMenu.attributes.add('EntidadInformaciones', { type: 'entity' });
BtnMenu.attributes.add('Player', { type: 'entity' });
BtnMenu.attributes.add('Menu', {type: 'entity'});
BtnMenu.attributes.add('playerSpeed', {type: 'number', default: 0, title: 'Player Speed'});
// initialize code called once per entity
BtnMenu.prototype.initialize = function() {
// Get the original button texture
this.originalTexture = this.entity.element.textureAsset;
// Whether the element is currently hovered or not
this.hovered = false;
// mouse events
this.entity.element.on('mouseenter', this.onEnter, this);
this.entity.element.on('mousedown', this.onPress, this);
this.entity.element.on('mouseup', this.onRelease, this);
this.entity.element.on('mouseleave', this.onLeave, this);
// touch events
this.entity.element.on('touchstart', this.onPress, this);
this.entity.element.on('touchend', this.onRelease, this);
};
BtnMenu.prototype.onEnter = function (event) {
this.hovered = true;
event.element.textureAsset = this.hoverAsset;
event.element.entity.children[1].element.color = new pc.Color(0.02745098039, 0.60784313725, 0.51764705882);
var c = new pc.Color(170, 91, 31);
//console.log(event);
// set our cursor to a pointer
document.body.style.cursor = 'pointer';
};
BtnMenu.prototype.onLeave = function (event) {
this.hovered = false;
event.element.entity.children[1].element.color = new pc.Color(1,1,1);
// go back to default cursor
document.body.style.cursor = 'default';
};
BtnMenu.prototype.onRelease = function (event) {
//A teletransportarse, ve al getentitydata y pilla el id, si el id es el mismo, manda la posición
//y de ahí debería teletransportarse
var id = this.entity.script.btnMenu.id;
for (var i = 0; i < this.EntidadInformaciones.children.length; i++) {
if(id == this.EntidadInformaciones.children[i].script.entityData.id) {
var position = new pc.Vec3(this.EntidadInformaciones.children[i].position.x,
this.EntidadInformaciones.children[i].position.y,
this.EntidadInformaciones.children[i].position.z);
//igual al atributo hijo del que ya teengo, ve a ese lado
this.Player.setPosition(position);
this.Player.rigidbody.teleport(position, pc.Vec3.ZERO);
var e = this.Menu;
e.enabled = !e.enabled;
}
}
};
BtnMenu.prototype.onPress = function (event) {
event.element.textureAsset = this.hovered ? this.hoverAsset : this.originalTexture;
};
If you use a dynamic rigidbody you can use this.entity.rigidbody.applyImpulse(0, 0, -1); and if you use a kinematic rigidbody you can use this.entity.translateLocal(0, 0, -1). (Based on your script you can replace “this.entity” for “this.Player” and you can replace “1” for “this.playerSpeed”).
To move the entity in the right direction you can use something like this.entity.lookAt(this.target.getPosition());, but this depends on your setup.
Hi @Albertos thanks for the answer I tried to apply it but it still doen’t move. The rigid body must be dynamic and the impulse it’s not working.
var BtnMenu = pc.createScript('btnMenu');
BtnMenu.attributes.add('id', {
type: 'number'
});
BtnMenu.attributes.add('EntidadInformaciones', { type: 'entity' });
BtnMenu.attributes.add('Player', { type: 'entity' });
BtnMenu.attributes.add('Menu', {type: 'entity'});
BtnMenu.attributes.add('playerSpeed', {type: 'number', default: 0, title: 'Player Speed'});
// initialize code called once per entity
BtnMenu.prototype.initialize = function() {
// Get the original button texture
this.originalTexture = this.entity.element.textureAsset;
// Whether the element is currently hovered or not
this.hovered = false;
// mouse events
this.entity.element.on('mouseenter', this.onEnter, this);
this.entity.element.on('mousedown', this.onPress, this);
//this.entity.element.on('mouseup', this.onRelease, this);
this.entity.element.on('mouseleave', this.onLeave, this);
// touch events
this.entity.element.on('touchstart', this.onPress, this);
this.entity.element.on('touchend', this.onRelease, this);
};
BtnMenu.prototype.onEnter = function (event) {
this.hovered = true;
event.element.textureAsset = this.hoverAsset;
event.element.entity.children[1].element.color = new pc.Color(0.02745098039, 0.60784313725, 0.51764705882);
var c = new pc.Color(170, 91, 31);
//console.log(event);
// set our cursor to a pointer
document.body.style.cursor = 'pointer';
};
BtnMenu.prototype.update = function(dt) {
this.entity.element.on('mouseup', this.onRelease, this);
};
BtnMenu.prototype.onLeave = function (event) {
this.hovered = false;
event.element.entity.children[1].element.color = new pc.Color(1,1,1);
// go back to default cursor
document.body.style.cursor = 'default';
};
BtnMenu.prototype.onRelease = function (event) {
//A teletransportarse, ve al getentitydata y pilla el id, si el id es el mismo, manda la posición
//y de ahí debería teletransportarse
var id = this.entity.script.btnMenu.id;
for (var i = 0; i < this.EntidadInformaciones.children.length; i++) {
if(id == this.EntidadInformaciones.children[i].script.entityData.id) {
var position = new pc.Vec3(this.EntidadInformaciones.children[i].position.x,
this.EntidadInformaciones.children[i].position.y,
this.EntidadInformaciones.children[i].position.z);
//igual al atributo hijo del que ya teengo, ve a ese lado
/*this.Player.setPosition(position);
this.Player.rigidbody.teleport(position, pc.Vec3.ZERO);*/
this.Player.rigidbody.applyImpulse(0, 0, this.playerSpeed);
var e = this.Menu;
e.enabled = !e.enabled;
}
}
};
BtnMenu.prototype.onPress = function (event) {
event.element.textureAsset = this.hovered ? this.hoverAsset : this.originalTexture;
};