Game Development Community

Dismounting a vehicle

by Rick Kelley · in Torque Game Engine · 03/17/2006 (8:43 am) · 5 replies

I have posted this question in the private area with no response, so if someone that has perhaps used the present version of the sdk and has scripted a clean dismounts that works, could you give me some insight into how it works. Presently I have tried older posts on dismounting, and it doesnt work for me. It seems that the consol commands allow it to work and will not call it again with a key bind with this portion of the script I have worked on to get rid of errors that show up using some of the scripting taken from various other scripts on the subject, (which by themselves will not work, hence my editing and combination):
function doPlayerDismount(%vehicle, %obj, %col)
{
   // This function is called by player.cc when the jump trigger
   // is true while mounted
   if (!%obj.isMounted())
      return;

   // Position above dismount point
   %pos    = getWords(%obj.getTransform(), 0, 2);
   %oldPos = %pos;
   %vec[0] = " 0  0  1";
   %vec[1] = " 0  0  1";
   %vec[2] = " 0  0 -1";
   %vec[3] = " 1  0  0";
   %vec[4] = "-1  0  0";
   %impulseVec  = "0 0 0";
   %vec[0] = MatrixMulVector( %obj.getTransform(), %vec[0]);

   // Make sure the point is valid
   %pos = "0 0 0";
   %numAttempts = 5;
   %success     = -1;
   for (%i = 0; %i < %numAttempts; %i++) {
      %pos = VectorAdd(%oldPos, VectorScale(%vec[%i], 3));
      if (%obj.checkDismountPoint(%oldPos, %pos)) {
         %success = %i;
         %impulseVec = %vec[%i];
         break;
      }
   }
   if (%forced && %success == -1)
      %pos = %oldPos;
//%obj.setTransform("0 0 0 0 0 1 0");

   // Position above dismount point
   %obj.setTransform(%pos);
   %obj.applyImpulse(%pos, VectorScale(%impulseVec, %obj.getDataBlock().mass));
   //%obj.setPilot(false);
   %obj.vehicleTurret = "";

%obj.unmount();
   echo("\n attempting a forced dismount");
%obj.setControlObject(%obj);

//if(%obj.mVehicle)
//%obj.mVehicle.getDataBlock().playerDismounted(%obj.mVehicle, %obj);
%obj.mountVehicle = false;
%obj.schedule(4000, "MountVehicles", true);


	//%obj.unmount();
	//%obj.setControlObject(%obj);
	//%obj.mounted = "0";
	//%obj.isMounted = false;
	//onPlayerUnMount();
}

This function will get called from the player.cs and the echo to the consol shows up:
echo("\n attempting a forced dismount");
and no errors are reported in the consol. Again this only seems to be called once, as the echo is not repeated again that makes me believe that this portion of the code is setting the flag to make this function return without continuing the rest of the block of script:
// This function is called by player.cc when the jump trigger
// is true while mounted
if (!%obj.isMounted())
return;

Any ideas or help is appreciated as I would like to dismount my snowmobile in my game instead of commiting suicide to do it!

#1
03/17/2006 (9:08 am)
It looks like you threw in a bunch of code in without understanding what any of it is doing.

First I would make sure that the %vehicle and %obj parameters you are passing in are in fact references to the player and vehicle objects. Then I would try changing
%obj.setControlObject(%obj);

to 

%obj.client.setControlObject(%obj);

And also make sure all the functions you are referencing in your code, do in fact exist.
#2
03/17/2006 (11:28 am)
Thanks for the reply, and I made the change and it works. Now it dismounts, but hanges on to the vehicle and I cant move away from it. If I try to persist in moving, it will remount the vehicle according to the setdelay time. Is there a way to modifiy it to place the player to the side away from the collision box?

As to throwing a bunch of code, yes I did, in attempt to get this working. It was the same for code to mount it, present post on how to do it wasnt quite correct or would not work straight out of the box as posted. It took me a day to get it to work by just commenting out lines, using echos etc. Some of the function call for things that do not seem to exist anymore in version 1.4, but work in an older demo I had before purchasing the current build and sdk. I wish there was a clean copy of this somewhere to document how to do a proper mount and dismount that works with 1.4

Perhaps after getting this figured out and cleaned up I will post it as a resource that will include my snowmobile resouce that seems to work acceptably for how I wont it, that includes a flamethrower :D
#3
03/17/2006 (1:35 pm)
Here's what I'm using and it works fine.
function HumanMaleAvatar::onMount(%this,%obj,%vehicle,%node)
{
   %obj.setTransform("0 0 0 0 0 1 0");
   %obj.setActionThread(%vehicle.getDatablock().mountPose[%node]);
   if (%node == 0)
   {
      %obj.setControlObject(%vehicle);
      %obj.lastWeapon = %obj.getMountedImage($WeaponSlot);
      %obj.unmountImage($WeaponSlot);
   }
}


function HumanMaleAvatar::onUnmount( %this, %obj, %vehicle, %node )
{
   %obj.mountImage(%obj.lastWeapon, $WeaponSlot);
}

function HumanMaleAvatar::doDismount(%this, %obj, %forced)
{
   // This function is called by the game engine when the jump trigger
   // is true while mounted

   // Position above dismount point
   %pos    = getWords(%obj.getTransform(), 0, 2);
   %oldPos = %pos;

   %vec[0] = " 1  1  1";
   %vec[1] = " 1  1  1";
   %vec[2] = " 1  1  -1";
   %vec[3] = " 1  0  0";
   %vec[4] = "-1  0  0";

   %impulseVec  = "0 0 0";
   %vec[0] = MatrixMulVector( %obj.getTransform(), %vec[0]);

   // Make sure the point is valid
   %pos = "0 0 0";
   %numAttempts = 5;
   %success     = -1;

   for (%i = 0; %i < %numAttempts; %i++)
   {
      %pos = VectorAdd(%oldPos, VectorScale(%vec[%i], 3));
      if (%obj.checkDismountPoint(%oldPos, %pos))
      {
         %success = %i;
         %impulseVec = %vec[%i];
         break;
      }
   }
   if (%forced && %success == -1)
      %pos = %oldPos;
   %obj.unmount();
   %obj.setControlObject(%obj);
   %obj.mountVehicle = false;

   // Position above dismount point
   %obj.setTransform(%pos);
   %obj.applyImpulse(%pos, VectorScale(%impulseVec, %obj.getDataBlock().mass));

}

Check it with your code to see what you are doing wrong.
#4
03/17/2006 (5:51 pm)
It works great now! Thanks both of you for your time and effort :D The line I now have different than yours, Mike, is the %obj.setControlObject(%obj); to what was suggested by Paul as well as to put the line you have for function HumanMaleAvatar::onUnmount() inside of my dodismount. I have other code to unmount the weapon when not using the vehicle that works. I have cleaned all other lines out that did not seem to effect the function properly. Now on to how to destroy the vehicle effectly and the sound of the veheicle seems to be a bit wanky.

Thanks again!
#5
03/18/2006 (6:24 am)
Glad you got it working. :)