Wednesday, November 20, 2013

[TUT-IV] Detecting Targets (peds, vehicles and objects)

Right now i will show the concept that i use to detect targeted factors in the game, i generally calc the distance amongst each possible target and the center of aim position.

Download the sample project here, let's use this project as the base for this tutorial.

We have an simple way to detect targeted peds, its the approach Player.GetTargetedPed:

With this strategy and an gun (something that will make you capable to see the aim camera, for instance an pistol or even an brick) you can detect the targeted ped, but its as well a lot imprecise, for instance if you aim at an automobile that has an driver, he will be detected as the target even if he is away from center of aim.
Also this strategy never detects the peds that are Dead or in Ragdoll state.

My notion consists in get all achievable targets, save then on an list, run this list and see who is closer to the center of the aim.

Very first lets develop our list of feasible targets, lets commence with peds, this will be an international object so we can access it in the tick:

Now lets fill this list when the player press the aim button, so in the keydown event lets check for appropriate mouse click and fill the list:

Now comes the primary component, run the list in the tick and calc who is closer to center of aiming:

So, what is taking place at every single line?

  • If Game.isGameKeyPressed(GameKey.Aim) AndAlso Exists(pedsToTarget) Then
here we check if the user is aiming and if the list is prepared to be employed

  • Dim dist As Double
  • Dim comparePosition As Vector3
right here we declare two variable to aid us with the code, "dist" will be the distance between the ped and the camera, "comparePosition" will be the closest position of the center of the camera associated to the ped position

  • For Each p As Ped In pedsToTarget
now we commence the For that will run the list

  • If Exists(p) AndAlso (p <> Player.Character) Then
check if ped exists and if is distinct from player character

  • dist = p.Position.DistanceTo(Game.CurrentCamera.Position)
calculates the distance between the ped and the camera

  • comparePosition = Game.CurrentCamera.Position + Game.CurrentCamera.Direction * dist
figure out the evaluate position, we will use this position to see if ped is close to center of aiming

  • If p.Position.DistanceTo(comparePosition) < .five Then
now we see if the distance between the ped position and the compare position is less than .5, if true this means that the ped is close adequate to be deemed the target, we can make it far more/significantly less precise altering the .5 value

  • p.ForceRagdoll(2000, Accurate)
  • p.ApplyForce(Vector3.WorldUp * .five, Vector3.WorldNorth)
  • msg("detected", ten)
here we just do some funny impact in the targeted ped to simply see who is targeted and show an message when the target is set, the outcome is an floating ped ^^

This is an exemplification of the calc:

The most significant difficulty with this approach is that we will get targeted peds that are behind walls and when the ped is fallen on ground the ped position is ~.five more than what we anticipate as ped pos, this indicates that you require to aim a small up when he is fallen on ground.

The same thought can be utilized with cars, but to be much more precise we will need to have to examine some added positions not just the center of the vehicle:

we will need this added variable in the tick:

we do the standard center position check:

and we evaluate to the "front position" too employing GetOffsetPosition to receive this position:

we multiply the Y (width) dimension of the model with .5 to get the half of the width and with relativeFront vector (a single step forward)

we do the same with back, top, left and correct position of the automobile:

This tends to make much more easy to target the car when it is big like a bus or a truck, i personally favor to verify just the center position, in excellent portion of circumstances is enough.

Download the Script of this tutorial.

[TUT-IV] Detecting Targets (peds, vehicles and objects)
9out of 10 based on 10 ratings. 9 user reviews.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.