Gradual Pitch in Script
by Chris "C2" Byars · in Torque Game Engine · 11/12/2005 (4:01 pm) · 12 replies
How would one go about adjusting the player's camera pitch gradually in script? Maybe adjusting it in small amounts over a specified time?
You see, I have at the moment a pitch(insert number here); command which bumps the pitch up in the onFire function for a few of my weapons. All fine and dandy except I want the pitch to go back to what it was before the pitch was altered. And I want this to happen smoothly. (Like in SWAT 4)
You see, I have at the moment a pitch(insert number here); command which bumps the pitch up in the onFire function for a few of my weapons. All fine and dandy except I want the pitch to go back to what it was before the pitch was altered. And I want this to happen smoothly. (Like in SWAT 4)
#2
Take a look at the 'schedule' function
11/12/2005 (4:19 pm)
You could make a server-side function that changed the pitch by a small fraction of the total you want, and then scheduled itself to be called again in say 20ms. It could stop when your pitch hit its target.Take a look at the 'schedule' function
#3
function yaw(%val)
{
$mvYaw += getMouseAdjustAmount(%val);
}
function pitch(%val)
{
$mvPitch += getMouseAdjustAmount(%val);
}
you can probably figure out what you need.
You'll want to set a variable to what both of them were before you modify them.
function movepitch(){
$targetpitch=20;
$MvPitch++;
if ($MvPitch!=$targetpitch)
schedule(10,0,movepitch);
}
there's some psudecode to move the pitch one way. would need something similiar to reverse it. 10 MS was just a number i picked.... probably too small
11/12/2005 (4:23 pm)
If you look at these functions in defaultbinds.cs....function yaw(%val)
{
$mvYaw += getMouseAdjustAmount(%val);
}
function pitch(%val)
{
$mvPitch += getMouseAdjustAmount(%val);
}
you can probably figure out what you need.
You'll want to set a variable to what both of them were before you modify them.
function movepitch(){
$targetpitch=20;
$MvPitch++;
if ($MvPitch!=$targetpitch)
schedule(10,0,movepitch);
}
there's some psudecode to move the pitch one way. would need something similiar to reverse it. 10 MS was just a number i picked.... probably too small
#4
11/12/2005 (4:31 pm)
@Stefan: Currently I just tossed in into the function yourWeaponImage::onFire(%this, %obj, %slot) this little snippet of pitch(-2);
#5
Basically just add in your weapon state of fire a stateTransitionOnTriggerUp which points to a state of adjusting the pitch.
Then make a new state
Then underneath all that weapon image datablock mumbo jumbo, add these:
And finally in your gunImage::onFire function add
And it should work. Works excellent for me.
11/12/2005 (5:04 pm)
I've done it. It's probably the wrong way to go about things but it's a cheap hack and it works.Basically just add in your weapon state of fire a stateTransitionOnTriggerUp which points to a state of adjusting the pitch.
stateTransitionOnTriggerUp[3] = "ItBePitchTime"; //this is in your fire state
Then make a new state
stateName[7] = "ItBePitchTime"; stateTimeoutValue[7] = 0.0; stateScript[7] = "adjustPitch"; stateTransitionOnTimeout[7] = "Activate";
Then underneath all that weapon image datablock mumbo jumbo, add these:
function movepitch1()
{
pitch(1);
}
function movepitch2()
{
pitch(1);
}
function movepitch3()
{
pitch(1);
}
function movepitch4()
{
pitch(1);
}
function movepitch5()
{
pitch(1);
}
function movepitch6()
{
pitch(1);
}
function XM8Image::adjustPitch(%this, %obj, %slot)
{
schedule(1,0,movepitch1);
schedule(50,0,movepitch2);
schedule(100,0,movepitch3);
schedule(150,0,movepitch4);
schedule(200,0,movepitch5);
schedule(250,0,movepitch6);
}And finally in your gunImage::onFire function add
pitch(-6);
And it should work. Works excellent for me.
#6
Edit: I would sugest to modify the Player code to allow for recoil effects.
11/12/2005 (6:47 pm)
Is this tested on a network? Pitch is a client side function that modifies the $mvPitch value which gets sent to the server in updateMove. Therefore, without any type of networking, this will only work when the client is the sever.Edit: I would sugest to modify the Player code to allow for recoil effects.
#7
11/12/2005 (6:54 pm)
You claimed a while back to have a script side command which allowed for pitch recoil, how does yours compare?
#8
We call a script method on the player that forces movement on his aim; it works great over the network. Simple yet effective.
11/12/2005 (7:17 pm)
Quote:We use a script command to force a pitch/yaw move on the player.
We call a script method on the player that forces movement on his aim; it works great over the network. Simple yet effective.
#9
The player's view is pitched, BUT (and this is the important part) if you're running a non-dedicated server (ie. where the server has a player too) that player (on the server) will also get his pitch altered, for some dumb reason I don't know.
I thought it might had been a client side function that I was running, but then it wouldn't had worked on the connecting client, because the script is ran on the server. So I'm left clueless.
The code for this is actually already in the moveManager.
02/01/2006 (12:59 am)
There's a slight problem with the approach I took, I don't know what the problem is technically.. but it works for me for the moment anyway.The player's view is pitched, BUT (and this is the important part) if you're running a non-dedicated server (ie. where the server has a player too) that player (on the server) will also get his pitch altered, for some dumb reason I don't know.
I thought it might had been a client side function that I was running, but then it wouldn't had worked on the connecting client, because the script is ran on the server. So I'm left clueless.
The code for this is actually already in the moveManager.
#10
02/01/2006 (5:39 am)
I figured that would happen.. hrm. I was hoping Midhir would be able to send me his implementation, which has a more realistic version. The aim is altered depending on how much you're firing, so single shots are accurate. Unfortunately he has lost internet for a while.
#11
I was talking about the function for actually applying the pitch, not the gameplay function which alters it.. that's not something you want to do in the same function, and it's something totally different and really out of scope of this thread.
Altering the pitch depending how you're fireing is as simple as setting a few variables in the onFire method. It's really simple.
Good luck
02/19/2006 (11:04 am)
Quote:
I was hoping Midhir would be able to send me his implementation, which has a more realistic version.
I was talking about the function for actually applying the pitch, not the gameplay function which alters it.. that's not something you want to do in the same function, and it's something totally different and really out of scope of this thread.
Altering the pitch depending how you're fireing is as simple as setting a few variables in the onFire method. It's really simple.
Good luck
#12
$recoilBurst = 0.00;
function recoilReset() {
$recoilBurst = 0.00;
return;
}
// Weapon recoil
function clientCmdRecoil(%l, %r, %u, %d)
{
%l = getRandom(0, (%l)*100);
%r = getRandom(0, (%r)*100);
%u = getRandom(0, (%u)*100);
%d = getRandom(0, (%d)*100);
%y = (-(%u+$recoilBurst))+(%d);
%x = (-%l)+(%r);
//echo ("pitch " @ %y);
//echo ("yaw " @ %x);
%x = %x/100;
%y = %y/100;
pitch(%y);
yaw(%x);
$recoilBurst += 10; // This number controls how much more recoil each shot will have.
cancel($recoilEvent);
$recoilEvent = schedule(250, 0, recoilReset); // This gets reset after 250 msec (1/4 second) of not firing.
return;
}
I then use the following in the onFire method of each gun:
commandToClient(%obj.client,'Recoil',%this.recoilLeft,%this.recoilRight, %this.recoilUp,%this.recoilDown);
And in the datablock of each weapon something like the following:
recoilLeft = 0.000;
recoilRight = 0.300;
recoilUp = 2.800;
recoilDown = 0.000;
Chris, sorry this took so long to post, I've been having some...life issues.
04/20/2006 (3:21 pm)
Not to hijack the thread, but I think this is the core of what you wanted, Chris. Everybody feel free to use this, take credit for it, become rich by selling it to the wealthy, or whatever. This is the recoil.cs script in my client scripts (I'm sure this is not secure, but it works):$recoilBurst = 0.00;
function recoilReset() {
$recoilBurst = 0.00;
return;
}
// Weapon recoil
function clientCmdRecoil(%l, %r, %u, %d)
{
%l = getRandom(0, (%l)*100);
%r = getRandom(0, (%r)*100);
%u = getRandom(0, (%u)*100);
%d = getRandom(0, (%d)*100);
%y = (-(%u+$recoilBurst))+(%d);
%x = (-%l)+(%r);
//echo ("pitch " @ %y);
//echo ("yaw " @ %x);
%x = %x/100;
%y = %y/100;
pitch(%y);
yaw(%x);
$recoilBurst += 10; // This number controls how much more recoil each shot will have.
cancel($recoilEvent);
$recoilEvent = schedule(250, 0, recoilReset); // This gets reset after 250 msec (1/4 second) of not firing.
return;
}
I then use the following in the onFire method of each gun:
commandToClient(%obj.client,'Recoil',%this.recoilLeft,%this.recoilRight, %this.recoilUp,%this.recoilDown);
And in the datablock of each weapon something like the following:
recoilLeft = 0.000;
recoilRight = 0.300;
recoilUp = 2.800;
recoilDown = 0.000;
Chris, sorry this took so long to post, I've been having some...life issues.
Torque Owner Stefan Lundmark