View Full Version : Getting a character to follow another

03-20-2009, 04:43 PM
Basically I am writing a simple game but I have got a problem. Part of it is that I am controlling the main character who walks up to a secondary character who is then meant to follow him. The code I have written is:

AI1H2 = (a + b) / 2; //AI1H2 is the X axis coordinate for the second character
AI1V2 = (c + d) / 2; //Y coordinate for computer character
if (MidH3 > b &amp;&amp; MidH3 < a &amp;&amp; MidV3 > c &amp;&amp; MidV3 < d) //MidH3 and MidV3 are the X and Y coordinates for the player controlled character
if (MidV3 < AI1V2)
c += 0.05; //c and d are the Y coordinates of secondary character
d += 0.05;
if (MidV3 > AI1V2)
c -= 0.05;
d -= 0.05;
if (MidH3 < AI1H2)
a += 0.05; //X coordinates of secondary character
b += 0.05;
if (MidH3 > AI1H2)
a -= 0.05;
b -= 0.05;

I have made sure and the if loops work when both characters are near each other but the second one doesn't move. Anyone know what I am doing wrong?

03-30-2009, 06:03 PM
You're setting the coordinates of the computer character before you changed a,b,c, and d. That could be the problem. I don't know what they are before coming into this bit though.