Make enemy chase player
by rennie moffat · in Torque Game Builder · 06/12/2009 (2:38 pm) · 5 replies
Hi, I am working with enemies, in the 2D shooter scroll tutorial, the one with space ships. I added a bunch of random functions/behaviors to my enemy's ship (enemy.cs). Specifically the faceObject, moveToward, behaviors thinking this would obviously help in game play.
When I run the program i was expecting to see the enemy ship, essentially chase the player ship. it does not. It just flies horizontally off the screen, reappearing at a new, random, vertical height, continues to fly thru repeating infinitely.
So, what am I doing wrong? I want my enemy to be aware of the player and chase him down no matter what.
EDIT:
I since I have thought about it.
I believe the necessary step is to some how relate/declare the player ship class(object?) to the particular functions of the enemy's ship. If I am right, must htis be done thru code only or is there a way thru the console?
Thanks for any help,
Ren
When I run the program i was expecting to see the enemy ship, essentially chase the player ship. it does not. It just flies horizontally off the screen, reappearing at a new, random, vertical height, continues to fly thru repeating infinitely.
So, what am I doing wrong? I want my enemy to be aware of the player and chase him down no matter what.
EDIT:
I since I have thought about it.
I believe the necessary step is to some how relate/declare the player ship class(object?) to the particular functions of the enemy's ship. If I am right, must htis be done thru code only or is there a way thru the console?
Thanks for any help,
Ren
About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
function enemy::onUpdate(%this) {
if(player.getPositionX() > %this.getPositionX()) {
%this.setLinearVelocityX(5);
if(player.getPositionX() < %this.getPositionX()) {
%this.setLinearVelocityX(-5);
if(player.getPositionY() > %this.getPositionY()) {
%this.setLinearVelocityY(5);
if(player.getPositionY() < %this.getPositionY()) {
%this.setLinearVelocityY(-5);
}
Also, for more realism, you might want to make the enemy ship move to a point in front of the player ship (so it can shoot at it or something). If so, just do "if(player.getPositionX() + 40 > etc etc..."
06/13/2009 (4:47 am)
Just make it so the enemy ship will move up when the player ship is above it, down when it's below it, etc. using the getPositionX and getPositionY functions. Your code might look something like this:function enemy::onUpdate(%this) {
if(player.getPositionX() > %this.getPositionX()) {
%this.setLinearVelocityX(5);
if(player.getPositionX() < %this.getPositionX()) {
%this.setLinearVelocityX(-5);
if(player.getPositionY() > %this.getPositionY()) {
%this.setLinearVelocityY(5);
if(player.getPositionY() < %this.getPositionY()) {
%this.setLinearVelocityY(-5);
}
Also, for more realism, you might want to make the enemy ship move to a point in front of the player ship (so it can shoot at it or something). If so, just do "if(player.getPositionX() + 40 > etc etc..."
#3
that makes sense.
One quick question tho regarding Torque script you might be able to answer. When coding, would I declare the name of the player class inside the script? ie. manually edit it?
Why I am wondering is, say I wanted to use this script but I did not want to chase the player, I wanted to chase the ball (an AI controlled object). Would I go in and manually change "player" to "ball"? Or would I do something with the console?
06/13/2009 (5:49 am)
Thanks man, that makes sense.
One quick question tho regarding Torque script you might be able to answer. When coding, would I declare the name of the player class inside the script? ie. manually edit it?
Why I am wondering is, say I wanted to use this script but I did not want to chase the player, I wanted to chase the ball (an AI controlled object). Would I go in and manually change "player" to "ball"? Or would I do something with the console?
#4
By the way, that code above checks the objects name, not it's class (probably not a good practice when you have multiple players, but it works).
06/13/2009 (6:10 am)
Just changing it to "ball" in the script should work fine. Or did you mean dynamically change it in-game?By the way, that code above checks the objects name, not it's class (probably not a good practice when you have multiple players, but it works).
#5
you said the code checks the objects name, not class. How do you know that? And how, if I wanted, could I check a class?
Thanks.
06/13/2009 (9:24 am)
No I meant in the script, not dynamically. you said the code checks the objects name, not class. How do you know that? And how, if I wanted, could I check a class?
Thanks.
Torque Owner rennie moffat
Renman3000