Game Development Community

Animation over animation solution

by Ice · in Artist Corner · 11/30/2008 (4:15 pm) · 1 replies

Hi! I see there are a lot of people here that don't know how to make animations like sword attacking, etc, to work while the player is running or walking, etc. I found a trick that can resolve this: (I will reffer to an attacking animation in the next stepps).

In your playerdata file (the file where you have the datablock with player model and sequences) add your attacking animation as sequence (if you don't have it) like this:

datablock TSShapeConstructor(PlayerDts)
{
   baseShape = "./player.dts";
   sequence0 = "./player_root.dsq root";
   sequence1 = "./player_forward.dsq run";
   sequence2 = "./player_back.dsq back";

   //[...] here are the other sequences - you need to place them in order

   sequence28 = "./player_standjump.dsq standjump";
   sequence29 = "./player_looknw.dsq looknw";

   // here you put your attack sequence and name it whatever you want
   // because we will create a function to call that (attack will be our name)
   [b]sequence30 = "./player_attack.dsq attack";[/b]
};         

function attack()
{
   // play the animation - we have the global ($) variable $gspPlayer which we
   // can use it to manipulate the player object - we will implement it at the end
   $gspPlayer.setActionThread(attack);

   // now we will do a trick - stop the player movement so the attack animation
   // can play without any problems
   [b]$mvLeftAction = 0;
   $mvRightAction = 0;
   $mvForwardAction = 0;
   $mvBackwardAction = 0;
   $mvUpAction = 0;
   $mvDownAction = 0;[/b]
}

You may want to save this (I saved it as player.cs in "root/server/") and make sure you load it from game.cs (also in "root/server/" folder). To do this open "game.cs" then search for "function onServerCreated()" and add the following line aniwhere there:

exec("./player.cs");

Now we need to implement that "$gpsPlayer" variable. To do this open again "game.cs" (if you closed it) and search for "function GameConnection::createPlayer", scroll down a little and change the code so it looks like this:

// Create the player object
   %player = new Player() {
      dataBlock = PlayerBody;
      client = %this;
   };
   
   // here we put the global variable to track our player
   [b]$gspPlayer = %player;[/b]
   MissionCleanup.add(%player);

The last thing we need to do is to asign a Key for this animation. Open the file "root/client/default.bind.cs" (or "config.cs" if you have your controls there") and search for a place to put your new Key. My advice is to search for "moveMap.bind( keyboard, space, jump );" and put our new code after that line so you don't forget where you put it:

// attack animation
   moveMap.bind( mouse, button0, attack );

The "mouse" parameter let torque know that is a mouse called control; "button0" is the mouse left click; "attack" is our function name.

Now you should be able to attack or do whatever you want to while your player is running or walking. The only issue is that your running / walking action is being interrupted. I think you could be able to check for keydown (keypressed) events so if it finds "w" pressed then you enable the running forward animation again. Any ideas?? :D

Have fun!

(sry for my bad english)

#1
11/30/2008 (5:30 pm)
I tried the following code with no results:

function PlayerBody::onKeyDown(%this)
{
   if (%this == "w")
      $player_dir = "w";
   else if (%this == "a")
      $player_dir = "a";
   else if (%this == "s")
      $player_dir = "s";
   else if (%this == "d")
      $player_dir = "d";
}

function attack()
{
   $gspPlayer.setActionThread(attack);
   $mvLeftAction = 0;
   $mvRightAction = 0;
   $mvForwardAction = 0;
   $mvBackwardAction = 0;
   $mvUpAction = 0;
   $mvDownAction = 0;

   //if(%actionMap.bind("w"))
   if ($player_dir == "w")
      echo("w");
   else if ($player_dir == "a")
      echo("a");
   else if ($player_dir == "s")
      echo("s");
   else if ($player_dir == "d")
      echo("d");
}