Jump to content

Having problem with aim offset


Recommended Posts

Hi, I have been delving into AI with Torque 3D, so that being the case I have been doing some of the different tutorials. I am having a problem with the aim of the bot from Steve Acastors Simple FPS Tutorial. Everything other than the aim of the bot works fine, I can spawn with different weapons (I used the Ryder and Lurker) and spawn at points I specify. The bots function fine, with the console logs showing the thinking echos. The aim of the bot shoots up and to the left completely missing the player target unless the player is almost on top of the bot. I have played with different numerical values in the bot script for aim offset and nothing seems to change or affect the bad aim. I am guessing perhaps it is being overridden somewhere somehow?

Any help or information would be great. I am using torque 3.8.

Link to comment
Share on other sites

I did that tutorial ages ago (literally years ago). I am doing Steves Acastors Simple FPS. I realize it is made for 1.2 (which I own) but I am attempting it on 3.8. As I said everything else works perfectly. Here is the function-

01.function AIPlayer::attackTarget(%this, %target)


03. echo(%this.getname() @ " attacking!");

04. %this.setAimObject(%target, "0 0 1.5");

05. %this.schedule(150, "shoot");

06. // a slight delay to make sure we're aiming at the target

07. //you could always use a raycast to see if the target is in the bot's sights instead



It does not matter what value I place, or where in the offset parameters, the Bot always and only shoots up and to the left. I have changed the value to 1, to .75 to 2, there is no perceptible change. Either something changed between 1.2 and 3.8 or I am missing something somewhere. Probably the latter. That's why I was asking about the aim offset being over ridden somewhere, somehow. To me it does not make sense. According to what I have researched, the tutorials I have done and what I know of the setAimObject I should not have this issue. And I should be able to edit the offset and see a difference. So it seems either something has changed, or I am missing something somewhere.

Link to comment
Share on other sites

Just did a quick test with the console commands and spawning a bot. The problem is still there with console commands. The setAimObject seems to not work with the console commands either. I have not changed anything in the game other than the tutorial bot scripts so this is a stock 3.8 level with nothing in it except the terrain. I am perplexed.

Link to comment
Share on other sites

Check eyeoffset: http://garagegames.com/community/resources/view/21614/1


datablock ShapeBaseImageData(AiLurkerWeaponImage)  
   // Basic Item properties  
   shapeFile = "art/shapes/weapons/Lurker/TP_Lurker.DAE"; 
   emap = true;  
   imageAnimPrefix = "Rifle"; 
   // Specify mount point & offset for 3rd person, and eye offset  
   // for first person rendering.  
   mountPoint = 0;  
   firstPerson = false;  
   useEyeNode = false;  
   animateOnServer = false;  
   eyeOffset = "0.0 0.6 -0.1"; // right/left forward/backward, up/down  
Link to comment
Share on other sites

Eye offset is one option - I just modified WeaponImage::onFire() to get the muzzle point and calc the aim vector from that for AI units:

function WeaponImage::onFire(%this, %obj, %slot)
    //echo("\c4WeaponImage::onFire( "@%this.getName()@", "@%obj.client.nameBase@", "@%slot@" )");
    // Make sure we have valid data
    if (!isObject(%this.projectile))
        error("WeaponImage::onFire() - Invalid projectile datablock");
    // Decrement inventory ammo. The image's ammo state is updated
    // automatically by the ammo inventory hooks.
    if ( !%this.infiniteAmmo )
        %obj.decInventory(%this.ammo, 1);
    // Get the player's velocity, we'll then add it to that of the projectile
    %objectVelocity = %obj.getVelocity();
    %numProjectiles = %this.projectileNum;
    if (%numProjectiles == 0)
        %numProjectiles = 1;
    if (%obj.isMemberOfClass("AIPlayer") && isObject(%obj.target))
        %vec = %obj.getVectorTo(%obj.target.position);
            %vec = VectorAdd(%vec, "0 0 "@%obj.aimOffset);
        %vec = VectorNormalize(%vec);
        // getting the straight ahead aiming point of the gun
        %vec = %obj.getMuzzleVector(%slot);
    for (%i = 0; %i < %numProjectiles; %i++)
        if (%this.projectileSpread)
            // We'll need to "skew" this projectile a little bit.  We start by
            // Then we'll create a spread matrix by randomly generating x, y, and z
            // points in a circle
            %matrix = "";
            for(%j = 0; %j < 3; %j++)
                %matrix = %matrix @ (getRandom() - 0.5) * 2 * 3.1415926 * %this.projectileSpread @ " ";
            %mat = MatrixCreateFromEuler(%matrix);
            // Which we'll use to alter the projectile's initial vector with
            %muzzleVector = MatrixMulVector(%mat, %vec);
            // Weapon projectile doesn't have a spread factor so we fire it using
            // the straight ahead aiming point of the gun
            %muzzleVector = %vec;
        // Add player's velocity
        %muzzleVelocity = VectorAdd(
        VectorScale(%muzzleVector, %this.projectile.muzzleVelocity),
        VectorScale(%objectVelocity, %this.projectile.velInheritFactor));
        // Create the projectile object
        %p = new (%this.projectileType)()
            dataBlock = %this.projectile;
        %p.initialVelocity = %muzzleVelocity;
        %p.initialPosition = %obj.getMuzzlePoint(%slot);
        %p.sourceObject = %obj;
        %p.sourceSlot = %slot;
        %p.client = %obj.client;
        %p.sourceClass = %obj.getClassName();

I then added this AIPlayer method:

function AIPlayer::getVectorTo(%this, %target)
    if (getWordCount(%pos) < 2 && isObject(%target))
        %pos = %target.getPosition();
    %z = getWord(%this.boundingBox, 2) / 2;
    %offset = "0 0" SPC %z;
    %vec = VectorAdd(%offset, %this.getPosition());
    %z = getWord(%target.boundingBox, 2) / 2;
    %offset = "0 0" SPC %z;
    %pos = VectorAdd(%offset, %pos);
    return VectorSub(%pos, %vec);
Link to comment
Share on other sites

@Johxz- Yes I found that forum post just after my last post here. obviously something is wrong with the setAimObject. Thank you for the link I will look into that as a possible solution/band aide. I was planning on finding other target solutions anyway, I suppose this just bumps up the timeline on that.

@rlranft- Thank you for showing me your solution to this issue, I will check into using that or something similar as a solution as well as checking out the eyeoffset. As an aside to you I will be doing some of your tutorials next, just wanted to thank you for providing them.

Out of a bit of concern is this going to be fixed soon? It does not bother me personally much, as I have been playing around with Torque off and on for years now, I love the engine. Spent most of the previous time learning about art assets and level/mission design, looking into the AI stuff now. I could see how a Torque beginner might find this extremely frustrating as the setAimObject is one of the few actual AI functions in Torque. To find it broken could turn off beginners from using the engine or learning more about it. I consider myself a beginner with Torque still, but I am happy with the engine, and I view problems like this as another way to learn more about Torque, and Torque scripting. I have been having a blast learning about basic AI scripting and I am now designing my own.

Thank you for responding you have both helped me out a lot.

Link to comment
Share on other sites

Update- I implemented the eyeoffset to the standard lurker just to test and the bots aim and hit now. I will use this for now and check out implementing and developing another solution in the future. At least this allows me to make a prototype level with AIBots that can aim and shoot and hit me.

Thanks for the help!

Link to comment
Share on other sites

yea something is wrong with the setAimObject I think that fix need to be included. The prototype I did was close combat for than reason I not see this :| I'm glad we can help.


turn off beginners from using the engine or learning more about it
this is true. As comment a side, the other day I read in Facebook someone said that he is using currently unity3d, but he wished it had some features of torque3d. :roll: :D

if you don't know someone develop a datablock plugin for unity :lol:

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...
  • 8 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...