[SOLVED] Help with camera realtivity

you need to re-assign the attribute:
Screenshot 2021-03-10 at 4.45.36 PM

1 Like

yup changing the capitol reset that attribute. Thank you both for the help. I am now not getting NO error, however my character is not turning at all still. But no errors! I’ll try seeing how to get the character to turn with camera some more and come back.

2 Likes

As far as I can see, this line in your initialize function is doing nothing outside your initialize function. If you want to use the variable hitbox everywhere, you have to add var hitbox; before your initialize function and add the line hitbox = this.hitbox; inside your initialize function.

1 Like

Thank you Albertos, I changed that. Can you help me get my logic correct. Currently i have a model childed to a capsule (hitbox). the model should rotate as the capsule rotates. I want the capsules forward to be the direction of the cameras forward.

Does that logic seem correct?

1 Like

If I understand you correctly, that seems like a good logic, but you probably have to rotate the model entity 180 degrees.

1 Like

Ok cool, I’m looking through the docs, and i see things like get local transform and such. What would be a good use here to get the camera’s forward facing?

1 Like

Unfortunately, I cannot give a sensible answer to this myself. You could search for an example project that uses this, or wait for a response from someone else.

ok so i decided to switch off that since it’s not working out and go with this instead: https://developer.playcanvas.com/en/tutorials/third-person-controller/
However when i port the scripts over i get this error

Error: Cannot read property ‘cameraMovement’ of undefined. Now i have everything setup exactly as the other project.

The script

var PlayerMovement = pc.createScript('playerMovement');

PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });

PlayerMovement.prototype.initialize = function () {
    var app = this.app;
    var camera = app.root.findByName('Camera');
    this.cameraScript = camera.script.cameraMovement;
};

PlayerMovement.prototype.update = function (dt) {
    var app = this.app;

    var forward = this.entity.forward;
    var right = this.entity.right;

    var x = 0;
    var z = 0; 

    if (app.keyboard.isPressed(pc.KEY_A)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }

    if (x !== 0 || z !== 0) {
        var pos = new pc.Vec3(x * dt, 0, z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());

        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);

        this.entity.rigidbody.teleport(pos, rot);
    }
};

There is a camera available to be found, and it’s named Camera.

Edit: Is there somewhere in the docs to read about defining, so i can stop getting this error? My understanding of defining is declaring a variable, is that incorrect?

1 Like

Based on the error that you get it looks like there is no script component on your camera.

1 Like

That’s really good to know that’s what it means. The odd thing is there is definitely THE script on my camera. I will try remaking the camera and see if that helps.

1 Like

Ok so remaking the camera seemed to fix the issue. But now when i load in it is freaking out all over the place.

1 Like

Are you remaking the project to develop yourself? Otherwise you can just fork the project and then you have a solid foundation for your game.

1 Like

Yes i’m using it to learn Play Canvas and develop a project my self. I just can’t figure out for the life of me why the camera is spinning uncontrollably.

1 Like

Probably something in the script that controls the camera.

1 Like

Figured it out, had to do with the position of two objects childed.

1 Like

Good job!

1 Like