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


角色控制代码
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
相关产品推荐
相关产品推荐

