Key Press to Mount Vehicle
by mb · 12/14/2006 (3:01 pm) · 11 comments
This will allow you to mount a vehicle with a key press while colliding with it.
To start with I added a function mountVehicle to defaultBind.cs clientside which calls CanMountVehicle1 when the key is pressed, and calls CanMountVehicle0 when the key is released. Those two functions are on the server in commands.cs we'll add them next.
This is where you change the key bind. I mapped it to the 'period' key, you can change to whatever you like.
Add to client/scripts/client/config.cs
And again in client/config.cs
Now in commands.cs on the server I added the two functions:
Then in the vehicles code: car.cs (or whatever your vehicle is) onCollision function I added:
to be safe I added:
Thats all. Enjoy.
To start with I added a function mountVehicle to defaultBind.cs clientside which calls CanMountVehicle1 when the key is pressed, and calls CanMountVehicle0 when the key is released. Those two functions are on the server in commands.cs we'll add them next.
// mount vehicle stuff
function mountVehicle(%val)
{
if (%val)
{
commandToServer('CanMountVehicle1');
echo ("Calling: CanMountVehicle1");
}
else
{
commandToServer('CanMountVehicle0');
echo ("Calling: CanMountVehicle0");
}
}
moveMap.bind(keyboard, ".", mountVehicle);This is where you change the key bind. I mapped it to the 'period' key, you can change to whatever you like.
Add to client/scripts/client/config.cs
moveMap.bind(keyboard, ".", mountVehicle);
And again in client/config.cs
moveMap.bind(keyboard, ".", mountVehicle);
Now in commands.cs on the server I added the two functions:
function serverCmdCanMountVehicle1(%client)
{
%client.canMount = 1; // key is being pressed
echo ("PRESS: %client.canMount: " @ %client.canMount);
}
function serverCmdCanMountVehicle0(%client)
{
%client.canMount = 0; // key is being pressed
echo ("RELEASE: %client.canMount: " @ %client.canMount);
}Then in the vehicles code: car.cs (or whatever your vehicle is) onCollision function I added:
function WheeledVehicleData::onCollision(%this,%obj,%col,%vec,%speed)
{
// Collision with other objects, including items
%client = %col.client;
if(!%client)
{
return;
}
if (%client.canMount == 1)
{
%obj.mountObject(%col, 0);
}
}Then in game.cs in the function: GameConnection::onClientEnterGame(%this)to be safe I added:
%this.canMount = 0;
Thats all. Enjoy.
About the author
#2
And,
How I do the Vehicle use the keys to Steering?
12/17/2006 (5:16 am)
Hi, how I do the character go out of the car ?And,
How I do the Vehicle use the keys to Steering?
#3
not sure if thats all you need to add...
12/17/2006 (10:16 am)
In server/player.cs : (taken from someone else code in the forums...) I changed the vectors, you can change it how you like. You hit spacebar to dismount.function Armor::doDismount(%this, %obj, %forced)
{
// 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] = " -1 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.unmount();
%obj.setControlObject(%obj);
if(%obj.mVehicle)
%obj.mVehicle.mountable = true;
%obj.mountVehicle = false;
%obj.schedule(4000, "mountVehicles", true); // Position above dismount point
%obj.setTransform(%pos);
%obj.applyImpulse(%pos, VectorScale(%impulseVec, %obj.getDataBlock().mass));
%obj.setPilot(false);
%obj.vehicleTurret = "";
}not sure if thats all you need to add...
#4
12/20/2006 (5:31 am)
Thanks mb, now, when the player mount vehicle, the camera stay with player... have some way to do the engine change the camera to the "cam" Node of the car and not of the player? And, have some way to hide the player when mount the vehicle?
#5
@emerson
I commented on your forum topic, but I'll post it here also:
You can add this to your vehicles datablock
useEyePoint = true;
You need a eye node and a cam node on your vehicle. You can also change the offset and fov, etc... look at vehicle.cc in the engine code.
cameraRoll = true;
cameraLag = 0;
cameraDecay = 0;
cameraOffset = 0;
I posted a way to hide the player but its probably not a good way to do it.
www.garagegames.com/mg/forums/result.thread.php?qt=55692
12/24/2006 (7:17 am)
Small update: I took out the player collision because it was redundent... and not being used. My code now looks like (in player.cs):function Armor::onCollision(%this,%obj,%col)
{
if (%obj.getState() $= "Dead")
return;
// Try and pickup all items
if (%col.getClassName() $= "Item")
%obj.pickup(%col);
}@emerson
I commented on your forum topic, but I'll post it here also:
You can add this to your vehicles datablock
useEyePoint = true;
You need a eye node and a cam node on your vehicle. You can also change the offset and fov, etc... look at vehicle.cc in the engine code.
cameraRoll = true;
cameraLag = 0;
cameraDecay = 0;
cameraOffset = 0;
I posted a way to hide the player but its probably not a good way to do it.
www.garagegames.com/mg/forums/result.thread.php?qt=55692
#6
12/24/2006 (8:22 am)
Thank You!!!! This Work Fine!!!
#7
I'm using your resource, but, I'm need to do one thing and I can't get it, I hope what you can help me.
when I use this resource, the uniques commands what make the car run is the same of the character in starter.fps ( moveforward and movebackward ), so, the car don't have the brake, I'm using the keys
W=moveforward, A=moveleft, S=moveright and D=movebackward for the character movement and the keys UP=moveforward, Down=movebackward, Left=turnleft and Right=turnright, I need to change the UP and DOWN keys to make my car have one brake, and, other key to do a HandBrake.
I've tried to do changing the "client/config.cs" of UP=moveforward to UP=accelerate and DOWN=movebackward to brake, but, with this modifications the car not run, the accelerate and brake only work in the starter.racing ? Have one idea of how I solve this problem ?
Thanks...
02/23/2007 (8:38 am)
Hi mb, here I'm again...I'm using your resource, but, I'm need to do one thing and I can't get it, I hope what you can help me.
when I use this resource, the uniques commands what make the car run is the same of the character in starter.fps ( moveforward and movebackward ), so, the car don't have the brake, I'm using the keys
W=moveforward, A=moveleft, S=moveright and D=movebackward for the character movement and the keys UP=moveforward, Down=movebackward, Left=turnleft and Right=turnright, I need to change the UP and DOWN keys to make my car have one brake, and, other key to do a HandBrake.
I've tried to do changing the "client/config.cs" of UP=moveforward to UP=accelerate and DOWN=movebackward to brake, but, with this modifications the car not run, the accelerate and brake only work in the starter.racing ? Have one idea of how I solve this problem ?
Thanks...
#8
02/27/2007 (8:26 am)
I haven't really looked at the starter.racing code so I don't know how they implemented that.
#9
and the binds:
hope this helps... ?
02/27/2007 (9:27 pm)
I took a look at starter.racing to see if I could find anything that might help. I found in starter.racing\client\scripts\default.bind.cs the brake function:function brake(%val)
{
$mvBackwardAction = %val * $movementSpeed;
}and the hand brake function:function handbrake(%val)
{
$mvTriggerCount2++;
}(edit) and the accelerate function:function accelerate(%val)
{
$mvForwardAction = %val * $movementSpeed;
}and the binds:
moveMap.bind( keyboard, w, accelerate ); moveMap.bind( keyboard, s, brake ); moveMap.bind( keyboard, up, accelerate ); moveMap.bind( keyboard, down, brake ); moveMap.bind( keyboard, space, handbrake );Also one more thing I saw was in the cars datablock:
engineBrake = 600; // Braking when throttle is 0 brakeTorque = 8000; // When brakes are applied
hope this helps... ?
#10
You also need to add the functions above to the vehicleDriverMap.cs file.
a discussion of the brakes issue and code snippets can be found here:
www.garagegames.com/mg/forums/result.thread.php?qt=4275
09/19/2007 (10:18 am)
RE Brakes: There is a trigger conflict with the player and wheeledVehicle, the player uses it to jump - the wheeled vehicle to brake. It takes a minor engine tweak (in either player.cc or wheeledVehilce.cc depending on the method you choose).You also need to add the functions above to the vehicleDriverMap.cs file.
a discussion of the brakes issue and code snippets can be found here:
www.garagegames.com/mg/forums/result.thread.php?qt=4275
#11
BTW I made a new resource for mounting thats much better: Cast Ray Use Button
09/29/2007 (11:54 am)
Cool, thanks Kent.BTW I made a new resource for mounting thats much better: Cast Ray Use Button

Torque Owner Mike Stoddart
Wasn't sure if you'd seen this:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=3925