我正在为MIT Scratch中的教育编写一个简单的游戏,并想让精灵转向另一个精灵(想想一个跟随我们英雄船的外星人船).我可以很容易地让外星人的船指向英雄:
point towards 'hero'
Run Code Online (Sandbox Code Playgroud)
但我真正想做的是这样更渐进的事情:
if alien direction (in degrees) > direction of hero: turn -2 degrees
if alien direction (in degrees) < direction of hero: turn 2 degrees
Run Code Online (Sandbox Code Playgroud)
那我如何确定'英雄的方向'?
不幸的是,似乎没有内置的方法来实现这一点,所以需要一些三角函数.要计算从精灵1到精灵2的方向,您可以计算x和y中从1到2的位移,然后使用该atan函数找到所需的角度:

由于您实际上想要相对于外星船所面向方向的方向,因此最好使用矢量积(又称交叉积):

这里的截图来自这个Scratch项目.
使用指向作为找出的方法:
set temp to direction
point towards hero
if temp > direction
set direction to temp-2
else if temp < direction
set direction to temp-2
else
set direction to temp
Run Code Online (Sandbox Code Playgroud)