[SOLVED] I have a problem with camera setting

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.

Hi @Onur_Ozturk,

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.

I hope this is helpful

2 Likes

hello, thank you for your answer. So how can I get him to look at the ball without moving the camera?

Oh!

You could use the lookAt() function on the camera:

https://developer.playcanvas.com/en/api/pc.Entity.html#lookAt

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));
     }

    else {
          this.entity.lookAt(this.Sphere.getPosition());
     }
};

I hope that is helpful

1 Like

Thank you so much. I’ll give it a try and report the result here. :slight_smile:

It’s working. Thank you so much :slight_smile:

1 Like

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.

1 Like

Hello @Albertos , my english is not very good unfortunately, i usually use “Google Translate”.

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).

1 Like

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. :sweat_smile:

I edited all the texts I quoted

1 Like

No problem, I already suspected something like that was the cause.

1 Like