Object movements
by Student-GameDevelopment · in Torque Game Engine · 07/31/2009 (10:37 pm) · 5 replies
Hello, I have the dungeon pack, and i got the goblin warrior to attack the player and deal damage, but, i dont understand how to tell torque to move to the players position minus a few units. At the moment, all i can do is have the goblin move to the player, which is not really possible due to shape collisions. So the Goblin AI basically goes crazy trying position the goblin propertly, the goblin starts spinning around lol.
This is what i have for code, how to i tell torque a positon a few units away from the player to position the goblin for battle?
//subtact the final destination by 2 units?
//this obviously does not work
%position = VectorSub(%tgtPlayer.getWorldBoxCenter(), "2 0 2");
%obj.setMoveDestination(%position);
Any ideas?
This is what i have for code, how to i tell torque a positon a few units away from the player to position the goblin for battle?
//subtact the final destination by 2 units?
//this obviously does not work
%position = VectorSub(%tgtPlayer.getWorldBoxCenter(), "2 0 2");
%obj.setMoveDestination(%position);
Any ideas?
About the author
#2
You also need to be sure that %tgtPlayer is the player's object not the connection.
08/03/2009 (11:36 am)
%position = VectorSub(%tgtPlayer.getWorldBoxCenter(), "2 0 2"); %obj.setMoveDestination(%position);This takes the players box's center position and subtracts two on the X and Z axis. That's global though so that means that if the goblin is on the wrong side of your player he will actually attempt to go through your player. Also the Z axis is your vertical axis. What you want is two units on the vector going to the goblin.
You also need to be sure that %tgtPlayer is the player's object not the connection.
// Get a vector from our player to the goblin(%obj) %vector = VectorSub(%obj.getPosition(),%tgtPlayer.getPosition()); // Normalize that vector then scale it by two units %vector = VectorScale(VectorNormalize(%vector),2); // Move from the player's position by the vector amount %position = VectorAdd(%tgtPlayer.getPosition(),%vector); // Set our destination %obj.setMoveDestination(%position);I just wrote that here so there may be some errors.
#3
So i need to scale the vector returned by the subtraction, which returns the vector of the player minus the goblin. Then you are nirmalizing and then scaling by 2 units. then you are adding that position to the players current position to get the final destination of 2 units in front of the player.
I think i get it, and i will put this to work.
Thanks Wes!
08/03/2009 (8:36 pm)
Wow, that looks like it might work. I new there was a way, i just didnt know how to explain to torque what i was trying to do.So i need to scale the vector returned by the subtraction, which returns the vector of the player minus the goblin. Then you are nirmalizing and then scaling by 2 units. then you are adding that position to the players current position to get the final destination of 2 units in front of the player.
I think i get it, and i will put this to work.
Thanks Wes!
#4
Now i have another issue, i am getting a console error when the goblin tries to attack. It is saying unknown command for setActionThread("attack"); which i tried both %obj.setActionThread("attack");
and %this.setActionThread("attack"); and both are comming back with an error. Obviously i have the wrong object being passed into my function for attacking, but now i am stuck figuring out how to tell what is a goblin object and what is not.
function GoblinBase::openFire(%this,%obj)
{
if(!isObject(%obj))
return;
%obj.playAudio(3,SwingAtSound);
if($DISTANCE_FROM_TARGET < 4.5)
{
%obj.setImageTrigger(0,true);
%obj.setActionThread("attack");
%this.schedule(400, "ceaseFire", %obj);
echo("Attacking!!");
%tgtPlayer.damage(%this, %obj, 3);
}
}
08/04/2009 (8:52 am)
Ok. That seems to work out good!Now i have another issue, i am getting a console error when the goblin tries to attack. It is saying unknown command for setActionThread("attack"); which i tried both %obj.setActionThread("attack");
and %this.setActionThread("attack"); and both are comming back with an error. Obviously i have the wrong object being passed into my function for attacking, but now i am stuck figuring out how to tell what is a goblin object and what is not.
function GoblinBase::openFire(%this,%obj)
{
if(!isObject(%obj))
return;
%obj.playAudio(3,SwingAtSound);
if($DISTANCE_FROM_TARGET < 4.5)
{
%obj.setImageTrigger(0,true);
%obj.setActionThread("attack");
%this.schedule(400, "ceaseFire", %obj);
echo("Attacking!!");
%tgtPlayer.damage(%this, %obj, 3);
}
}
#5
What do i need to do so my AI can return to its spawn location when giving up on the attack?
Thanks
08/11/2009 (6:39 pm)
Ok. I got the positioning math done right for attacking the player, now i have another issue. In the AI, when the player goes out of attack range, the goblin gives up and starts running back to its spawn location. there is where the same issue happens again. I sent the goblin back to its spawn origin with a setmovedestination. The goblin turns arround and runs towards that spot, and just a hair away from the destination, which is the spawn origin it is to return to, it starts going crazy again. Its acting as though it cant return to its spawn origin like something is blocking it. I ran an echo trace, and onreachdestination is never called..What do i need to do so my AI can return to its spawn location when giving up on the attack?
Thanks
Torque 3D Owner Justin Campbell
For example;