简体   繁体   English

Game Maker-如何为弹丸创建归位效果?

[英]Game Maker - How do I create homing effect for a projectile?

I'm trying to make a homing projectile for my bullet hell game and I'd need to be able to calculate the angle between the target and projectile relatively to the projectile's angle (0 degrees would be the direction the projectile is pointing). 我正在尝试为我的子弹地狱游戏制作归巢的弹丸,并且我需要能够计算出目标和弹丸之间相对于弹丸角度的角度(0度将是弹丸指向的方向)。 Right now the angle calculation is absolute done using point_direction , but the problem is when the target is at the 4th sector the projectile starts steering the wrong way. 现在,角度计算是使用point_direction绝对完成的,但问题是,当目标位于第4扇区时,弹丸开始以错误的方式转向。 Another issue is that if the projectile does a 180 degree turn while chasing the target (or moves down if fired by enemy) the steering direction will get inverted. 另一个问题是,如果射弹在追逐目标时旋转了180度(如果被敌人发射则向下移动),转向方向将反转。 I have also tried mp_potential_ functions but their pathfinding is too "agressive". 我也尝试过mp_potential_函数,但是它们的寻路太“激进”了。

在此处输入图片说明

This is what my current code looks like: 这是我当前的代码如下所示:

if(instance_exists(obj_fighter1)) {
    var target;
    target = instance_nearest(x, y, obj_fighter1);

    if(target != noone) {
        var angle_to_target;
        angle_to_target = point_direction(x,y,target.x,target.y);

        if(angle_to_target < direction) {
            direction -= 2;
        }

        if(angle_to_target > direction) {
            direction += 2;
        }
    }
}

Hopefully this information is enough and is understandable. 希望这些信息足够并且可以理解。

Okay, a common Game Maker question. 好吧,这是Game Maker的常见问题 The routine I use is below. 我使用的例程如下。 Looking at it, it could do with a bit of refactoring, but it does work. 来看它,它可以进行一些重构,但是确实有效。

var wantDir;
var currDir;
var directiondiff;
var maxTurn;

// want - this is your target direction \\
wantDir = argument0;

// max turn - this is the max number of degrees to turn \\
maxTurn = argument1;

// current - this is your current direction \\
currDir = direction;

if (wantDir >= (currDir + 180))
{
    currDir += 360;
}
else
{
    if (wantDir < (currDir - 180))
    {
        wantDir += 360;
    }
}

directiondiff = wantDir - currDir;

if (directiondiff < -maxTurn)
{
    directiondiff = -maxTurn
}

if (directiondiff > maxTurn)
{
    directiondiff = maxTurn
}

return directiondiff

So you'd call this, and it'll return you a value that you can add to your missile's angle. 因此,您称其为“否”,它将为您返回一个可以添加到导弹角度的值。 So if you call it scr_get_angle , your code might then look like this: 因此,如果将其scr_get_angle ,则代码可能如下所示:

if(instance_exists(obj_fighter1)) {
    var target;
    target = instance_nearest(x, y, obj_fighter1);

    if(target != noone) {
        var angle_to_target;
        angle_to_target = point_direction(x,y,target.x,target.y);

        direction += scr_get_angle(angle_to_target, 2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM