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

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

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.

public class EnemyController : MonoBehaviour
{
    public float enemySpeed = 6f;               // The patrol speed of the enemy
    public float rotationSpeed = 5f;           // The rotation speed of the enemy

    private Rigidbody2D rb;
    private Vector2 moveToTarget;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        SetRandomPatrolTarget();
    }

    private void FixedUpdate()
    {
        // Determine the direction and angle to the target
        Vector2 directionToTarget = moveToTarget - rb.position;
        float angleToTarget = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;

        // Calculate the angle difference
        float angleDifference = Mathf.DeltaAngle(rb.rotation, angleToTarget);

        if (Mathf.Abs(angleDifference) > 0.5f)
        {
            // Rotate towards the target position
            rb.MoveRotation(rb.rotation + Mathf.Sign(angleDifference) * rotationSpeed * Time.fixedDeltaTime);
        }
        else
        {
            // Move towards the target position
            rb.velocity = transform.up * enemySpeed;
        }
    }

    private void SetRandomPatrolTarget()
    {
        // Adjust the range of the patrol target position to fit your game world
        float minX = -10f;
        float maxX = 10f;
        float minY = -5f;
        float maxY = 5f;

        moveToTarget = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0f);
    }

    private void OnDrawGizmos()
    {
        // Draw attack range gizmo
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(moveToTarget, 0.25f);
    }
}

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.

public class EnemyController : MonoBehaviour
{
    public float enemySpeed = 6f;               // The patrol speed of the enemy
    public float rotationSpeed = 5f;             // The rotation speed of the enemy

    private Rigidbody2D rb;
    private Vector2 moveToTarget;

    private void Start()
    {
        rb = GetComponent&lt;Rigidbody2D&gt;();
        SetRandomPatrolTarget();
    }

    private void FixedUpdate()
    {
        // Determine the direction and angle to the target
        Vector2 directionToTarget = moveToTarget - rb.position;
        float angleToTarget = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;

        // Calculate the angle difference
        float angleDifference = Mathf.DeltaAngle(rb.rotation, angleToTarget);

        if (Mathf.Abs(angleDifference) &gt; 0.5f)
        {
            // Rotate towards the target position
            rb.MoveRotation(rb.rotation + Mathf.Sign(angleDifference) * rotationSpeed * Time.fixedDeltaTime);
        }
        else
        {
            // Move towards the target position
            rb.velocity = transform.up * enemySpeed;
        }
    }

    private void SetRandomPatrolTarget()
    {
        // Adjust the range of the patrol target position to fit your game world
        float minX = -10f;
        float maxX = 10f;
        float minY = -5f;
        float maxY = 5f;

        moveToTarget = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0f);
    }

    private void OnDrawGizmos()
    {
        // Draw attack range gizmo
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(moveToTarget, 0.25f);
    }
}

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:

确定