When the ball(Sphere) hits an obstacle I want the camera to stop tracking the ball(Sphere) but watch from afar.
The “CameraController” script content is like this for now.
var CameraController = pc.createScript('cameraController');
CameraController.attributes.add('Sphere',{
type: 'entity'
});
CameraController.attributes.add('offset',{
type: 'vec3'
});
// initialize code called once per entity
CameraController.prototype.initialize = function() {
this.offset = this.entity.getPosition().sub(this.Sphere.getPosition());
};
// update code called every frame
CameraController.prototype.update = function(dt) {
this.entity.setPosition(this.Sphere.getPosition().add(this.offset));
};
How can I do this on “CameraController”?
Thank you for your help in advance.
I’m assuming you’re using physics to move the ball. My immediate thought would be to monitor the ball for collision events using something like:
CameraController.prototype.initialize = function() {
//...
this.followBall = true;
this.Sphere.rigidbody.on('collisionstart', this.onCollision, this);
};
CameraController.prototype.onCollision = function(result) {
//Add logic to check which entity was collided with here
this.followBall = false;
};
CameraController.prototype.update = function(dt) {
if(this.followBall) {
this.entity.setPosition(this.Sphere.getPosition().add(this.offset));
}
};
Of course this code would fire anytime the ball hits any other entity, so you would want to add a way to make sure you’re capturing the correct collisions. You could do this with tags, an array of items, or even the objects’ names.
Another way to consider would be firing app wide events from the objects that cause the camera to stop following and then flip the variable in the camera script to stop the camera from following.
Hi @Onur_Ozturk! I don’t know how you do it, but can you try to keep the quotes in original (English) language. Right now it’s a little confusing for other users.
I understand, but keep the quote that you use in your post original please, because otherwise other users can’t read the quote. (It also looks like, the user that you quote write something in your language, which is not true).
oh!, I understand that when you automatically translate the page, it takes the translation language when quoting, sorry I pay attention to that. I’ve realized now.