setPosition moves particle emitter but not whole entity

Project link: PlayCanvas | HTML5 Game Engine
Code link: PlayCanvas | HTML5 Game Engine
Referring to: ball.js

Hi all, I’m a teacher, new to PC, trying to create a 3D version of Pong as a potential class project. I’m at the step of trying to have the ball reset to the center if it goes past a certain x-coordinate (eventually this will be off-screen, right now it’s near the left edge, just for testing). Right now, nothing I try seems to work - I can get the ball to stop moving when it moves past a particular x coordinate, but it won’t jump back to its starting location (0, 5, 0). Even more strangely, the particle emitter DOES move, but leaves the ball behind. I’m using this (in ball.js):

// MoveBall.prototype.update = function(dt) {
//
// if (this.entity.position.x < -5) {
// console.log(“Ball should freeze and move to starting location at this point”);
//
// var linVel = this.entity.rigidbody.linearVelocity;
// linVel.y = 0;
// linVel.x = 0;
// linVel.z = 0;
// this.entity.rigidbody.linearVelocity = linVel; // This DOES work, no problem here
//
// var pos = this.entity.getPosition();
// pos.x = 0;
// pos.y = 5;
// pos.z = 0;
// this.entity.setPosition(pos); // This DOESN’T work, moves only particle emitter, not whole entity
//
// }
// };

Thanks for any help you might be able to offer!

If you are trying to set the position of a dynamic rigid body, you have to use the teleport function via the rigid body component

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#teleport

More information here: https://developer.playcanvas.com/en/user-manual/physics/physics-basics/#teleporting-dynamic-bodies

1 Like

That worked perfectly. Thank you!!

1 Like