Helle. I have a problem with the script how to move a camera.I want to use the script to control the camera move.And this is my script.
Camera.prototype.initialize = function() {
var game=this.app.root.findByName('Root').script.game;
this.app.on('camera:move',function(){
var tmp=new pc.Vec3(0,0,0);
tmp.copy(game.getPos());
var tp=new pc.Vec3(3,3,3);
var tp2=tmp.add(tp);
this.entity.setPosition(tp2);
},this);
};
I use this.entity.setPosition(tp2); to control the camera position.However,i found this movement is so stiff.So,i want to know that is it have a method to control the camera movement just like JQ $.animate()?
My English is bad,so i am so sorry if you can’t understand what i say.
Perhaps one way would be to move the setPosition to the update function which simply moves the camera in small increments towards the position you want to move it to.
One note here is that pc.Vec3#add will change the vector it’s called from. So here tp will be added to tmp. Then doing tp2 = tmp.add(tp) is the same as if you did
tmp.add(tp);
var tp2 = tmp;
That’s just a side note
Now to your question. If you want to move objects you should do it in the update method of the script and always multiply the speed with dt (short for delta time). That way the speed will be constant no matter what the frame rate is. If you wanted to move an object towards some direction you could do something like this:
MyScript.prototype.initialize = function () {
this.direction = new pc.Vec3();
this.speed = 1;
};
MyScript.prototype.update = function (dt) {
this.direction.set(1, 0, 0) // just an example;
var pos = this.entity.getPosition();
pos.add(this.direction.scale(this.speed * dt));
this.entity.setPosition(pos);
};