在直视玩家之前停止敌人旋转

问题描述 投票:0回答:1

所以问题是,我在 3D 环境中处理 2D 精灵,当精灵直接垂直于相机时,你看不到它。现在,我设法通过跟踪让播放器工作

mousePos
;

 if (lookAtMouseEnabled)
        {
            Lookatmouse();
        }
        Vector3 mousePos = Input.mousePosition;
        //Debug.Log($"Mouse X: {mousePos.x}");

        if (mousePos.x >= minXRotation && mousePos.x <= maxXRotation)
        {
            lookAtMouseEnabled = false;
        }
        else
        {
            lookAtMouseEnabled = true;
        }

但是,我不相信我可以对敌人做同样的事情?我正在考虑跟踪玩家是否在左侧或右侧,并在敌人直视玩家之前停止旋转

20f
,反之亦然;

 public Transform player;
    public float speed = 5.0f;
    public float rotationSpeed = 10.0f;
    public float chaseDistance = 2.0f;
    public float stoppingDistance = 1.0f;

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        if (player == null)
        {
            GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
            if (playerObject != null)
            {
                player = playerObject.transform;
            }
        }

        if (player != null)
        {
            Vector3 direction = player.position - transform.position;
            float distance = direction.magnitude;

            if (distance > stoppingDistance)
            {
                //Rotation
                direction.Normalize();
                Quaternion lookRotation = Quaternion.LookRotation(direction);
                rb.rotation = Quaternion.Slerp(rb.rotation, lookRotation, rotationSpeed * Time.fixedDeltaTime);

                //ChasePlayer
                Vector3 chasePosition = player.position - player.forward * chaseDistance;
                Vector3 newPosition = Vector3.MoveTowards(rb.position, chasePosition, speed * Time.fixedDeltaTime);
                rb.MovePosition(newPosition);

                // Determine if the player is on the left or right
                Vector3 enemyToPlayer = player.position - transform.position;
                Vector3 enemyRight = transform.right; 
                float dotProduct = Vector3.Dot(enemyToPlayer, enemyRight);

                if (dotProduct < 0)
                {
                    Debug.Log("Player is on the left.");
                }
                else
                {
                    Debug.Log("Player is on the right.");
                }
            }
        }
    }

我只是想知道如何解决我为自己制作的这个 2D 精灵 3D 世界复杂问题。任何建议都会很棒。

c# unity-game-engine
1个回答
0
投票

我不确定我是否完全理解您想要实现的目标。如果您很难在 3D 世界中为 2D 精灵设置正确的方向,您可以尝试使用 LookAt() 方法强制游戏对象看着相机。

© www.soinside.com 2019 - 2024. All rights reserved.