2D Player look at mouse on 2D plain

Hello, I am at a block, used to play canvas had a tutorial for 3D objects to point to the mouse, though the tutorial is now gone, and I have no conception of how to conclude this. If anyone will be so considerate and help please let me understand.

Hi @Jacob_Mcbride,

Can you describe the problem you are having? What is not working?

1 Like

Sure, I have no idea as how to make a script for this.

How would I use this code?

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

/**
 * Initialize
 * @returns {FollowMouse}
 */
FollowMouse.prototype.initialize = function() {
    this.cameraEntity = this.app.root.findByName('Camera');
    this.mousePosition = new pc.Vec3();
    this.mouseScreenPosition = new pc.Vec3();
    this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
    return this;
};

/**
 * Update
 * @param {number} dt
 * @returns {FollowMouse}
 */
FollowMouse.prototype.update = function(dt) {
    if(this.cameraEntity){
        this.cameraEntity.camera.screenToWorld(this.mouseScreenPosition.x, this.mouseScreenPosition.y, this.cameraEntity.getPosition().y, this.mousePosition);   
    }
    
    this.mousePosition.y = this.entity.getPosition().y; 
    this.entity.lookAt(this.mousePosition);
    return this;
};

/**
 * Mouse move handler
 * @param {object} event - mouse event 
 * @returns {FollowMouse}
 */
FollowMouse.prototype.onMouseMove = function (event) {
    this.mouseScreenPosition.x = event.x;
    this.mouseScreenPosition.y = event.y;
    return this;
};