SOLVED Enemy shoots at Player's Z co-ordinate

Tryin Ryan

Member
Hi, I'm trying to make a 3D doom-like game in GMS 1.4.

So far, I can make enemies shoot a projectile at the player's X and Y co-ordinates fine, but I cannot figure out how to make the enemy shoot at the player's Z co-ordinate. For example, if the player is higher than the enemy, the enemy will shoot up toward the player.
I've tried testing several ideas, and I've tried looking through the forums for a solution, but I've had no luck.
 

TheouAegis

Member
Help me out with some hard values. Right after (edit: the last line does no good before the code, lol) that code, put this (change any variables as necessary):
Code:
show_debug_message("("+string(x)+","+string(y)+","+string(z));
show_debug_message("("+string(player.x)+","+string(player.y)+","+string(player.z));
show_debug_message(shooting_angle)  //or whatever variable you used
Run the code, copy one of those printouts so I can see what's happening.
 
Last edited:

FrostyCat

Redemption Seeker
GML:
// Calculate facing vector
var _dx = obj_player.x-x;
var _dy = obj_player.y-y;
var _dz = obj_player.z-z;

// Scale it down to match shoot_speed
var _dvscale = shoot_speed/point_distance_3d(0, 0, 0, _dx, _dy, _dz);
var _vx = _dx*_dvscale;
var _vy = _dy*_dvscale;
var _vz = _dz*_dvscale;

// Create the bullet and set its velocity
with (instance_create(x, y, obj_bullet)) {
    z = other.z;
    vx = _vx;
    vy = _vy;
    vz = _vz;
}
I strongly recommend that you stop and learn what vector addition/subtraction, vector scaling, vector norms, matrix products, dot products and cross products are. 3D game development has a very low tolerance for people whose vector geometry is not up to scratch.
 

Tryin Ryan

Member
Help me out with some hard values. Right after (edit: the last line does no good before the code, lol) that code, put this (change any variables as necessary):
Code:
show_debug_message("("+string(x)+","+string(y)+","+string(z));
show_debug_message("("+string(player.x)+","+string(player.y)+","+string(player.z));
show_debug_message(shooting_angle)  //or whatever variable you used
Run the code, copy one of those printouts so I can see what's happening.
x = 128,y = -224,z = 32
player.x = -38.96,player.y = -227.20,player.z = 31
shooting_angle = -0.34

Math looks good, but the bullet still shoots up in the air.
 

Tryin Ryan

Member
GML:
// Calculate facing vector
var _dx = obj_player.x-x;
var _dy = obj_player.y-y;
var _dz = obj_player.z-z;

// Scale it down to match shoot_speed
var _dvscale = shoot_speed/point_distance_3d(0, 0, 0, _dx, _dy, _dz);
var _vx = _dx*_dvscale;
var _vy = _dy*_dvscale;
var _vz = _dz*_dvscale;

// Create the bullet and set its velocity
with (instance_create(x, y, obj_bullet)) {
    z = other.z;
    vx = _vx;
    vy = _vy;
    vz = _vz;
}
I strongly recommend that you stop and learn what vector addition/subtraction, vector scaling, vector norms, matrix products, dot products and cross products are. 3D game development has a very low tolerance for people whose vector geometry is not up to scratch.
Haha fair enough! I have 5 years of experience in GameMaker 2D, but about a month of experience in 3D. I'll definitely stop and learn about the things you recommend. I'll also try your code, then let you all know if it works out.
 
Top