Game Development Community

Script Help - $Reward

by Jason Barno · in Torque Game Engine · 07/15/2007 (5:22 pm) · 5 replies

My latest project was coming along so nicely but my team appears to have hit a brick wall on two tasks.

1. Activateable Objects - Anyone familiar with Morrowind modding will instantly recognize this. I need a raycast setup so that the player can look at an object (crosshair over it) and press Space to call it's ::onActivate function. It's probably so simple but we just can't get our heads around the raycast implementation.

2. Raycast HUD - We need a raycast to check if you are looking at a player or item or activatable object that returns a unique value for each so that we can push a HUD object to show information on the target (Players would show Name, Level, Class, Etc. while items would show Name, Value, Damage, etc and activatable objects would simply show a name.) We know how to implement the GUI part, we just need a way to know when the crosshair moves over one of said objects.

Any help would be greatly appreciated and we'd be more than happy to throw a few dollars your way.

~Jace

#1
07/15/2007 (9:08 pm)
I can give you a good head start with the ray casting.

Chuck this wherever you need it - perhaps your weapons' onFire(%this, %obj, %slot) function:
// muzVec is the vector coming from the gun's "muzzle"
   %muzVec = %obj.getMuzzleVector(%slot);
   // muzNVec = normalized muzVec
   %muzNVec = VectorNormalize(%muzVec);
   // range of ray
   %range = 100;
   // scale muzNVec to the range the beam can reach
   %muzScaled = VectorScale(%muzNVec, %range);
   // muzPoint = the actual point of the gun's "muzzle"
   %muzPoint = %obj.getMuzzlePoint(%slot);
   // rangeEnd = muzzle point + length of beam
   %rangeEnd = VectorAdd(%muzPoint, %muzScaled);
   // search for just about anything
   %searchMasks = $TypeMasks::ShapeBaseObjectType | $TypeMasks::StaticShapeObjectType |
                  $TypeMasks::TerrainObjectType   | $TypeMasks::InteriorObjectType    |
                  $TypeMasks::StaticObjectType;
   // search for objects within the beam's range that fit the masks above
   %scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);

   if(%scanTarg)
   {
      %serverID = getWord(%scanTarg,0);
      switch$(%scanTarg.getClassname())
      {
         case Player:
            echo("Player is targeting another player - Server ID = " @ %serverID);
            // Do stuff
         case WheeledVehicle:
            echo("Player is targeting a vehicle - Server ID = " @ %serverID);
            // Do stuff
         case FlyingVehicle:
            echo("Player is targeting a vehicle - Server ID = " @ %serverID);
            // Do stuff
         case HoverVehicle:
            echo("Player is targeting a vehicle - Server ID = " @ %serverID);
            // Do stuff
         case InteriorInstance:
            echo("Player is targeting a building - Server ID = " @ %serverID);
            // Do stuff
         case TerrainBlock:
            echo("Player is targeting the ground - Server ID = " @ %serverID);
            // Do stuff

         default:
            // Do stuff
      }
   }
The rest is up to you - I've given you a function that will provide you with all the information you need, you just have to work out what you want to do with it.

Good luck!
#2
07/16/2007 (12:13 am)
Wow I needed the exact same thing and like magic there it is! Thanks Tim =)
#3
07/16/2007 (11:41 am)
That's amazing and it works perfect for what I need. The only thing that I still need related to this is how to add an ActivateableObjectType to the engine that calls the object::OnActivate when a button is pressed. I think I can do the button press stuff, but if anyone has any pointers as to adding a new ObjectType with just that one function, that would be awesome. One question, though, is there any way to use something than muzzlevector, simply because in my game you spend a lot of time with no weapon equipped, and I'd hate the HUDs to not work during that time. I already changed it to a 300ms tick rather than OnFire, but if I could just change the vector to maybe use the EyeVector or something? Or would that not work with the crosshair?

Edit : : If anyone can help me get this working, I *will* post an Activateable Object resource before the end of the day.
#4
07/16/2007 (11:47 am)
Can you work with the engine code? If you can, take a look at the advanced crosshair resource.
#5
07/16/2007 (12:16 pm)
It's strange, when I was looking, I even looked at that resource and misunderstood what it did. Turns out thats exactly what we need. Thanks everyone for your help, much appreciated. And thanks to Tim for giving us a better understanding of the RayCast system.

~Jace