Fixing entity to camera in FPS


#1

How can I fix a entity to camera view so that the entity moves with camera. Please help!!!
https://playcanvas.com/editor/scene/589092 here is the link to my game.


#2

Putting it as child of camera?


#3

Tried but its doesn’t work…
I can look everywhere but without a gun in front of me.


#4

Ah i see, when you turn around the gun don’t move. Wait i check what you have done


#5

Thank you so much in advance.


#6

For what i see the issue is that the gun follow the alignment of Player Cap that’s the main entity

    if (x !== 0 && z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);

in this part of code you set the angles of camera to move the view and the force that’s applied to the main entity to move along the way the camera is facing, so since the gun follow the main entity direction when camera rotate the gun stay still. You can try rotate the gun as well like
this.camera.Handgun.setLocalEulerAngles(this.eulers.y, this.eulers.x,0);
but don’t know if it will work, since i have no experience with fps, maybe you will need to adjust the localPosition of handgun as well


#7

Ok let’s see!!! !!!