Turn for the mouse cursor

Rotate Y over the course of the mouse …

var FollowMouse = pc.createScript('followMouse');

var mousePosition = new pc.Vec3();
    
FollowMouse.prototype.initialize = function() {
    this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
};

FollowMouse.prototype.update = function(dt) {
    var playerPosition = this.entity.getPosition();
    mousePosition.y = playerPosition.y; // Коррекция по вертикали если близко
    this.entity.lookAt(mousePosition);
    // this.entity.rotate(0, 0, 0); // Базовый угол поворота перед началом отслеживания
    
    var diff = new pc.Vec3();
    diff.sub2(mousePosition, playerPosition);
};

FollowMouse.prototype.onMouseMove = function (event) {
    var cameraEntity = this.app.root.findByName('Camera');
      if (cameraEntity)
          cameraEntity.camera.screenToWorld(event.x, event.y, cameraEntity.getPosition().y, mousePosition);
};

how to get in real time the coordinates of the mouse?

I’m not quite sure what you are asking? Are you looking for a way to the get the mouse position on the screen during the update function?

1 Like

Yes. I would be very grateful.

You just copy the mouse position from the mouse move function to a variable.

ie:

var FollowMouse = pc.createScript('followMouse');

var mousePosition = new pc.Vec3();
    
FollowMouse.prototype.initialize = function() {
    this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);

    this.mouseScreenPosition = new pc.Vec3();
};

FollowMouse.prototype.update = function(dt) {
    var playerPosition = this.entity.getPosition();
    mousePosition.y = playerPosition.y; // Коррекция по вертикали если близко
    this.entity.lookAt(mousePosition);
    // this.entity.rotate(0, 0, 0); // Базовый угол поворота перед началом отслеживания
    
    var diff = new pc.Vec3();
    diff.sub2(mousePosition, playerPosition);
};

FollowMouse.prototype.onMouseMove = function (event) {
    this.mouseScreenPosition.x = event.x;
    this.mouseScreenPosition.y = event.y;   

    var cameraEntity = this.app.root.findByName('Camera');
      if (cameraEntity)
          cameraEntity.camera.screenToWorld(event.x, event.y, cameraEntity.getPosition().y, mousePosition);
};
1 Like

I don’t understand the problem? Do you have a project you can share and can you explain what you want to happen?

Do the understand the changes I’ve made in the code that I’ve posted?

1 Like

Here is the project with the code I posted earlier working: https://playcanvas.com/project/527764/overview/project-from-forums

1 Like

Thank you very much helped me.

I understood what my mistake was…