Simple Follower

back

Notes

The object will follow wherever you point.

Source

/* @pjs preload="player.png"; */

PImage player;

float MAX_SPEED = 5;
float MIN_DISTANCE = 32;

float follower_x;
float follower_y;
float velocity_x;
float velocity_y;

// Setup the example
void setup(){
  size(640, 480);
  
  // Load images
  player = loadImage("player.png");
}

void draw(){
  background(#4488cc);
  
  float distance = dist(follower_x, follower_y, mouseX, mouseY);
  if(distance > MIN_DISTANCE){
    float rotation = atan2(mouseY - follower_y, mouseX - follower_x);
    
    velocity_x = cos(rotation) * MAX_SPEED;
    velocity_y = sin(rotation) * MAX_SPEED;
    
  }else{
    velocity_x = velocity_y = 0;
  }
  
  follower_x += velocity_x;
  follower_y += velocity_y;
  
  image(player, follower_x, follower_y);
}

Warning: Cannot load module "http" because required module "raphf" is not loaded in Unknown on line 0