我正在开发塔防游戏,我正在使用模板。
我想制作像(部落冲突)这样的 2d 塔防游戏,所以我想知道如何使用像(部落冲突中的佳能)这样的框架制作指向对象的炮塔。
我的意思是当一个物体进入塔的范围时,塔将指向它而不旋转塔,而是使用二维帧而不是通过某种方式使用代码或数学方式。
我找到了解决方案。做这个 :
float Direction = 0;
float FinalDirection = 0;
float DirectionDegree = 0;
int NumberOfDirections = 0; // eg: 24 or 32 or even 128 or anything Directions
DirectionDegree = 360 / NumberOfDirections;
void update() // this will run every frame
{
Direction = Math.atan2(target.y - tower.y, target.x - tower.x ) * (180 / Math.PI);
if(Direction < 0)
{
Direction += 360;
}
FinalDirection = Direction / DirectionDegree;
tower.frame = FinalDirection;
}