[英]enemy ship rotates towards the player but does not lock into position
當我的敵艦向玩家移動時,我發現他們之間的距離
private float FindDistance(Vector2 heroCenter, Vector2 spritePos)
{
var deltaX = Math.Pow((spritePos.X - heroCenter.X), 2); // the 2 is the power value (into the power of 2)
var deltaY = Math.Pow((spritePos.Y - heroCenter.Y), 2);
float distance = (float)Math.Sqrt(deltaX + deltaY);
return distance; // returns the distance between the two objects
}
然后我用這段代碼計算敵艦的旋轉角度
distanceFromHero = FindDistance(Constants.heroCenter, spritePos);
if (distanceFromHero < Constants.HeroWithinRange)
{
heroClose = true;
VelocityX *= .985f; // enemy reduces vel X
VelocityY *= .985f; // enemy reduces vel Y
//atan2 for angle
var radians = Math.Atan2((spritePos.Y - Constants.heroCenter.Y), (spritePos.X - Constants.heroCenter.X));
//radians into degrees
rotationAngle = (float)(radians * (180 / Math.PI));
}
else
{
heroClose = false;
}
但是奇怪的是,敵人雖然朝着玩家移動,但並沒有鎖定並保持穩定,而是像鍾擺一樣擺動,當敵人在同一地點時,它們不斷旋轉。 一些代碼幫助會有所幫助。
我在XBox論壇中找到了我的問題的完美答案,該論壇無法加載到Visual Studio 2017中,所以我提出了一個新的解決方案。 希望其他人可以從Microsoft編寫的這份出色的教程中受益。 解決方案在這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.