Hi there, I’m trying to code a switch from third to first person camera view, the camera uses raycastendpoint and it is attached to a player, my plan is to change raycast Z position ( as close to player as possible) and set the player model to invisible so it looks like it is first person, the problem persists when I move from 0,0,0 location, other times it works as it should
if(app.keyboard.wasReleased(pc.KEY_X) ){
rayEnd = this.app.root.findByName('RaycastEndPoint');
var pos2 = this.player.getPosition();
rayEnd.setPosition(pos2.x, pos2.y, pos2.z = 0.01);
console.log(pos2);
Maybe there’s better ways? multiple camera?
Full codes if needed
var CameraMovement = pc.createScript('cameraMovement');
CameraMovement.attributes.add('mouseSpeed', { type: 'number', default: 1.4, description: 'Mouse Sensitivity' });
// Called once after all resources are loaded and before the first update
CameraMovement.prototype.initialize = function () {
this.eulers = new pc.Vec3();
this.touchCoords = new pc.Vec2();
var app = this.app;
app.mouse.on("mousemove", this.onMouseMove, this);
app.mouse.on("mousedown", this.onMouseDown, this);
this.rayEnd = app.root.findByName('RaycastEndPoint');
this.on('destroy', function() {
app.mouse.off("mousemove", this.onMouseMove, this);
app.mouse.off("mousedown", this.onMouseDown, this);
}, this);
};
CameraMovement.prototype.postUpdate = function (dt) {
var originEntity = this.entity.parent;
var targetY = this.eulers.x + 180;
var targetX = this.eulers.y;
var targetAng = new pc.Vec3(-targetX, targetY, 0);
originEntity.setEulerAngles(targetAng);
this.entity.setPosition(this.getWorldPoint());
this.entity.lookAt(originEntity.getPosition());
};
CameraMovement.prototype.onMouseMove = function (e) {
if (pc.Mouse.isPointerLocked()) {
this.eulers.x -= ((this.mouseSpeed * e.dx) / 60) % 360;
this.eulers.y += ((this.mouseSpeed * e.dy) / 60) % 360;
if (this.eulers.x < 0) this.eulers.x += 360;
if (this.eulers.y < 0) this.eulers.y += 360;
}
};
CameraMovement.prototype.onMouseDown = function (e) {
this.app.mouse.enablePointerLock();
};
CameraMovement.prototype.getWorldPoint = function () {
var from = this.entity.parent.getPosition();
var to = this.rayEnd.getPosition();
var hitPoint = to;
var app = this.app;
var hit = app.systems.rigidbody.raycastFirst(from, to);
return hit ? hit.point : to;
};