Jump to content

Lock aim on entity


Stona Persona
 Share

Recommended Posts

Greetings fellas, 

im currently trying to figure out how to build a player system like in the PS1 era survival horror games like Resident Evil for example. 

So far i got static cameras to switch, tank controls and a very basic shooting mechanic (hold down R mouse and press L mouse to shoot).

But now im kinda stuck at how to approach a system that lets my player lock onto an enemy when aiming. Some help with pseudo code would be much appreciated.

Would i try to get distances between player and the closest enemy entity or would i use a pick with maybe a bigger radius so you kinda have to roughly aim into the enemies direction and if pick hits the player is locked on? Im still trying to figure the code stuff out, so not a pro coder here. :D

Link to comment
Share on other sites

I think the pick idea would work better.  Like you say, you would have to roughly aim at the enemy first.  Are you using C++ or LUA?  I haven't used Leadwerks in ages and right now I'm pressed for time, but here's the general idea (syntax is off, I'm using the tippy top of my head) :P

Something like this perhaps... there would be more to it than this.

float radius = 0.25f;
PickInfo pick_info;
Vec3 player_pos = player->GetPosition(true);
Vec3 target_position = camera->UnProject(screen_centre);//or Project??  I forget

if(Pick(player_pos, target_pos, pick_info, radius) == true){
	if(pick_info.entity == enemy) {
     	Vec3 enemy_pos = pick_info.entity->GetPosition(true);
      	player->Point(enemy_pos + an_offset_maybe)
    }
}

 

You could use distance as well and get the angle to each enemy to figure out the closest in distance and angle...

  • Like 1
Link to comment
Share on other sites

59 minutes ago, SpiderPig said:

I think the pick idea would work better.  Like you say, you would have to roughly aim at the enemy first.  Are you using C++ or LUA?  I haven't used Leadwerks in ages and right now I'm pressed for time, but here's the general idea (syntax is off, I'm using the tippy top of my head) :P

Something like this perhaps... there would be more to it than this.

float radius = 0.25f;
PickInfo pick_info;
Vec3 player_pos = player->GetPosition(true);
Vec3 target_position = camera->UnProject(screen_centre);//or Project??  I forget

if(Pick(player_pos, target_pos, pick_info, radius) == true){
	if(pick_info.entity == enemy) {
     	Vec3 enemy_pos = pick_info.entity->GetPosition(true);
      	player->Point(enemy_pos + an_offset_maybe)
    }
}

You could use distance as well and get the angle to each enemy to figure out the closest in distance and angle...

Thanks for the quick answer, i think i try it out like this. Im using LUA btw, should ve mentioned in first post. Since i use the player to cast a ray out in the world to detect stuff, so i dont plan to use a crosshair i think i wont need the Camera:UnProject but just get the position of the enemy from the pick. 

The thing is im trying to figure stuff out for so long now i kinda got tired of trying stuff that doesnt work out in the end, so im very thankful for your information. 

I think ill try to use the pick i already have when aiming and shooting to make it lock onto the picked enemy.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
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.

 Share

×
×
  • Create New...