You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

俯视角第三人称射击游戏角色未按世界轴正确移动问题

俯视角第三人称射击微游戏角色移动异常问题

我正在开发一个俯视角第三人称射击类微游戏项目,目前遇到角色移动异常问题,以下图文及代码可辅助说明:

Unity界面截图
问题说明手绘示意图

角色控制代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;

public class EntityController : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] private float maxAccelTime = 2;
    [SerializeField] private Camera currentCam;
    [SerializeField] private Rigidbody2D currentPlayer;

    private Vector2 cursorPos;
    private Vector2 lookDir;
    private float lookAngle;

    private float accelTime, accelMultipier;
    private float dragMultipier;
    
    void Start()
    {
        accelTime = 0;
        accelMultipier = 15;
        dragMultipier = 10;
    }

    // Update is called once per frame
    void Update()
    {
        //Control Movement Button
        MovementController();
        //Control Attack Button
        AttackController();
    }
    private void MovementController()
    {
        //Control speed of the player. Player will get faster as long as they pressing the button and not stop moving immediately when stop pressing.
        InitiateAceleration();
        //Control look direction and movement of the player.
        InitiateMovement();
    }
    private void AttackController()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Initiate Attack on Mouse Direction
        }
    }
    private void InitiateMovement()
    {
        //Control look direction of the player.
        cursorPos = currentCam.ScreenToWorldPoint(Input.mousePosition);
        lookDir = cursorPos - currentPlayer.position;
        lookAngle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;

        //Control movement direction of the player.
        currentPlayer.transform.Translate(Vector2.right * Input.GetAxis("Horizontal") * accelTime * accelMultipier * Time.deltaTime);
        currentPlayer.transform.Translate(Vector2.up * Input.GetAxis("Vertical") * accelTime * accelMultipier * Time.deltaTime);

        currentPlayer.rotation = lookAngle;
    }
    private void InitiateAceleration()
    {
        if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
        {
            if (accelTime < maxAccelTime)
            {
                accelTime += Time.deltaTime;
            }
        }
        else
        {
            if (accelTime > 0)
            {
                accelTime -= (Time.deltaTime * dragMultipier);
            }
        }
    }
}

已尝试的无效方案

  • 添加lookDir.x>0且lookDir.y>0的移动触发条件(推测角色靠近光标时的位置问题导致抖动),但未解决问题。

内容的提问来源于stack exchange,提问作者AmericanoOnTheRound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:30:14