Game Development Community

To raycast or not to raycast that is the question

by Ken Johnston · in Torque 3D Professional · 12/21/2009 (2:29 pm) · 2 replies

so quick question, i am trying to set attack animations based upon the direction the player is currently moving. For example if he moves forward and attacks play this attack animation, if he jumps and then attacks play this animation. My first thought was to cast a ray and pull a directional vector from that ray, but I am having alot of trouble getting this to work. Is there a better way to get the current players movement direction vector that i can hook into with a if then statement? Or should i just keep hacking at using a cast ray for determining the players direction vector? hope that makes sense, i am still a C++ noob and hopefully i got my point across....

#1
12/21/2009 (2:37 pm)
Why are you using raycasts, when the movement direction is the same as the velocity? You want something like this (script):

%player = MyPlayer;
%velocity   = %player.getVelocity();
%forwardVec = %player.getForwardVector();
%rightVec   = VectorCross(%forward, "0 0 1");

if (VectorDot(%velocity, %forwardVec) > 0)
  %forward = true;
else
  %forward = false;

if (VectorDot(%velocity, %rightVec) > 0)
{
  %right = true;
  %left = false;
}
else
{
  %right = false;
  %left = true;
}

//Do something based on %left, %right and %forward booleans...
#2
12/21/2009 (2:44 pm)
thanks manoel, that was exactly what i was looking for, I knew it had to be something much more simple, and something that was already keeping track of the movement direction...never occurred to me that it was in the velocity..though i should have after all the derivative of position is velocity...sheesh.