Following mouse pointer using java script or co blocks

I saw the other post of this subject and was wondering if it is possible to use co blocks to follow a mouse pointer. My teacher wants us to have a object follow the mouse pointer around like in the ice skating game. But he doesn’t want to make it that complicated for the students to make.

His instructions are:

Create a world and an object (like a ball but you can use any object) that will move around when the user clicks it and holds the mouse.

Get at least one object to constantly moves towards the ball. You can modify the speeds and animations depending on whether or not the ball is moving.

Use this to create a game of some sort. When you finish, try and see what else you can do with the mouse and make your world more interactive.

Hi @Jackson_Wilson,

Dragging-functionality like that can’t be made with CoBlocks right now. You would need to use JavaScript for that. Below is a space that shows how. Click the remix button to check it out :slight_smile:

Space explanation:

  • The first coding tab you can see is called DragLogic. It adds the dragging-behavior on the cube with the function .addMoveCollisionInteraction();.
  • The second coding tab is called FollowItem. It’s made with CoBlocks. Here, we turn the dog to look at the cube. If the dog is too far away, it’ll run closer to the cube. You could replace the dog-item with any other item

Please keep in mind that the code used for this behavior is internal and may change in the future. But can use it like this right now :slight_smile:

I hope this can help you getting started!

thank you this helps a lot!!!

1 Like

this code is broken…the mouse does not do anything the addMoveInteraction attribute fails

a bit of research in the api solved it…
new method in api is .baseitem.input.setItemDrag(target)

example
const cube1 = Scene.getItem(“osnFcMzD”) as BaseItem;
const floor = Scene.getItem(“jDKx2uRr”) as Cuboid;
cube1.input.setItemDrag(floor);

here is an example

1 Like