How do I make an object follow another?


#1

I want to make an object that fires projectiles at another object;and it follows it from any angle


#2

Hi @Aariv

You make the projectile point towards the object and move forward.

Point towards is done using some trig. See linked post for example code.

You’ll then need two separate code paths. One to loop the point towards code, and another to execute a Move Forward block with a big number. (If you had one code path alternating between point towards & a small move forward, it would work but the movement would be a little jerky)


#3

Read the answer posted by @ThinBuffalo, it is really helpful.


#4

I use this concept in some of my games and tests. Try this project out to see how it works.


#5

Thanks @ThinBuffalo,but can you simplify it?It looks easy,but I didn’t understood what you meant.


#6

I don’t only want to point it at something,but also want it fire a projectile in the direction of another object.


#7

Is it like the code checks the position of the circle,somehow calculates the angle needed to point toward the circle,and then does the work?and inverse tangents changes the angle of the real tangent.How does inverse tangent help?and why inverse?why not the real one?


#8

Okay, this may not work, but I did something similar earlier today.

When Thing is following
Set angle to (arctan((object being followed y-object following y)/(object being followed x-object following x)))

When Thing is following
Move forward (number)

#9

@Aariv

Sine, cosine, and tanget give the relationship between the length of 2 sides and an angle of a right triangle
Sin(theta)=O/H
Cos(theta)=A/H
Tan(theta)=O/A
Which to use just depends on what you know.

In Hopscotch I use inverse tanget much more than tanget. This is because I know O & A and want to know theta.

  • O is usually the change in Y position between two objects
  • A is usually the change in X position between two objects
  • Theta is the angle, so

theta = inverse tan(O/A)

:point_up_2: That’s all the formula is doing. Except the Check If add’s 180 depending on whether the object being pointed to is the right or the left of the object doing the pointing.


#10

I thought you wanted the projectile to point at & follow some target object. Can you explain more what you’re trying to do?


#11

For example,I want the thing to always point to the target but the projectile to be only be fired in that direction where the thing is pointing.I don’t want to make the projectile follow the target


#12

Easy fix.

Don’t use the

Repeat forever
 Set angle arctan...

Just make it say

When fired //do NOT use a conditional here
 Set angle arctan...
 Repeat [forever]
  Move forward