如何让我的敌人三角形移动到特定位置?

huangapple go评论120阅读模式
英文:

How do I get my enemy triangle to move to a specific position?

问题

I'm currently using Unity and trying to start off with a basic enemy for a top-down space shooter, where the enemy simply always moves forward but will rotate to go to a new position. This will be randomly generated, essentially a patrol route.

The issue that I keep having is that I can get the enemy to move, I can get them to rotate, but I cannot get them to rotate to the exact spot I want them to move towards. No matter how I try it, the enemy will rotate, but the right side of the triangle will face the position, and then it will either move forward and stay on that trajectory or it will continuously rotate around the target.

  1. public class EnemyController : MonoBehaviour
  2. {
  3. public float enemySpeed = 6f; // The patrol speed of the enemy
  4. public float rotationSpeed = 5f; // The rotation speed of the enemy
  5. private Rigidbody2D rb;
  6. private Vector2 moveToTarget;
  7. private void Start()
  8. {
  9. rb = GetComponent<Rigidbody2D>();
  10. SetRandomPatrolTarget();
  11. }
  12. private void FixedUpdate()
  13. {
  14. // Determine the direction and angle to the target
  15. Vector2 directionToTarget = moveToTarget - rb.position;
  16. float angleToTarget = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;
  17. // Calculate the angle difference
  18. float angleDifference = Mathf.DeltaAngle(rb.rotation, angleToTarget);
  19. if (Mathf.Abs(angleDifference) > 0.5f)
  20. {
  21. // Rotate towards the target position
  22. rb.MoveRotation(rb.rotation + Mathf.Sign(angleDifference) * rotationSpeed * Time.fixedDeltaTime);
  23. }
  24. else
  25. {
  26. // Move towards the target position
  27. rb.velocity = transform.up * enemySpeed;
  28. }
  29. }
  30. private void SetRandomPatrolTarget()
  31. {
  32. // Adjust the range of the patrol target position to fit your game world
  33. float minX = -10f;
  34. float maxX = 10f;
  35. float minY = -5f;
  36. float maxY = 5f;
  37. moveToTarget = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0f);
  38. }
  39. private void OnDrawGizmos()
  40. {
  41. // Draw attack range gizmo
  42. Gizmos.color = Color.red;
  43. Gizmos.DrawWireSphere(moveToTarget, 0.25f);
  44. }
  45. }

This is what I currently have. I've tried a few different things. I've tried asking ChatGPT for assistance and it doesn't seem to understand what I mean when I tell it what is going wrong.

如何让我的敌人三角形移动到特定位置?

Any advice as to what the issue is would be greatly helpful; I'm absolutely stumped.

英文:

I'm currently using Unity and trying to start off with a basic enemy for a top down space shooter, where the enemy simply always moves forward but will rotate to go to a new position. This will be randomly generated, essentially a patrol route.

The issue that I keep having is that I can get the enemy to move, I can get them to rotate, but I cannot get them to rotate to the exact spot I want them to move towards. No matter how I try it, the enemy will rotate, but the right side of the triangle will face the position and then it will either move forward and stay on that trajectory or it will continuously rotate around the target.

  1. public class EnemyController : MonoBehaviour
  2. {
  3. public float enemySpeed = 6f; // The patrol speed of the enemy
  4. public float rotationSpeed = 5f; // The rotation speed of the enemy
  5. private Rigidbody2D rb;
  6. private Vector2 moveToTarget;
  7. private void Start()
  8. {
  9. rb = GetComponent&lt;Rigidbody2D&gt;();
  10. SetRandomPatrolTarget();
  11. }
  12. private void FixedUpdate()
  13. {
  14. // Determine the direction and angle to the target
  15. Vector2 directionToTarget = moveToTarget - rb.position;
  16. float angleToTarget = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;
  17. // Calculate the angle difference
  18. float angleDifference = Mathf.DeltaAngle(rb.rotation, angleToTarget);
  19. if (Mathf.Abs(angleDifference) &gt; 0.5f)
  20. {
  21. // Rotate towards the target position
  22. rb.MoveRotation(rb.rotation + Mathf.Sign(angleDifference) * rotationSpeed * Time.fixedDeltaTime);
  23. }
  24. else
  25. {
  26. // Move towards the target position
  27. rb.velocity = transform.up * enemySpeed;
  28. }
  29. }
  30. private void SetRandomPatrolTarget()
  31. {
  32. // Adjust the range of the patrol target position to fit your game world
  33. float minX = -10f;
  34. float maxX = 10f;
  35. float minY = -5f;
  36. float maxY = 5f;
  37. moveToTarget = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0f);
  38. }
  39. private void OnDrawGizmos()
  40. {
  41. // Draw attack range gizmo
  42. Gizmos.color = Color.red;
  43. Gizmos.DrawWireSphere(moveToTarget, 0.25f);
  44. }
  45. }

This is what I currently have. I've tried a few different things. I've tried asking ChatGPT for assistance and it doesn't seem to understand what I mean when I tell it what is going wrong.

如何让我的敌人三角形移动到特定位置?

Any advice as to what the issue is would be greatly helpful, i'm absolutely stumped.

答案1

得分: 1

The expression Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg; seems to be the source.
The rest of your code works on the angle being from upwards (0, 1), where this is giving it to you relative to right (1, 0). So it's always 90 degrees off.

To fix it, you could just subtract 90 from the above expression.
Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg - 90

I would recommend this instead though. It's easier to read. Vector2.SignedAngle(Vector2.up, directionToTarget). It gives the same result.

英文:

The expression Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg; seems to be the source.
The rest of your code works on the angle being from upwards (0, 1), where this is giving it to you relative to right (1, 0). So it's always 90 degrees off.

To fix it, you could just subtract 90 from the above expression.
Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg - 90

I would recommend this instead though. It's easier to read. Vector2.SignedAngle(Vector2.up, directionToTarget). It gives the same result.

huangapple
  • 本文由 发表于 2023年7月17日 22:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定