[英]Unity: Diagonal movement not corresponding to correct key inputs
一段时间以来,我一直在努力寻找解决此问题的方法,但是没有运气。
我希望能够在8个方向上移动,但出于某些奇怪的原因,我的玩家只想在6个方向上移动。
当我按:
W + D或W + A,它将移至右上角。
S + D或S + A,它向左下方移动。
垂直和水平运动都很好。 这只是四个对角线运动中的两个而已。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerTest : MonoBehaviour
{
public float moveSpeed;
private Animator anim;
private Rigidbody2D playerRigidbody;
private bool playerMoving;
public Vector2 lastMove;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
playerMoving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
playerRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, playerRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.y, Input.GetAxisRaw("Vertical") * moveSpeed);
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
playerRigidbody.velocity = new Vector2(0f, playerRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
}
这是我用于角色移动的基本代码。 如果有人可以帮助我解决此问题,将不胜感激。
谢谢,Redza
整个逻辑对我来说似乎很奇怪,我会做出一个单一的运动向量
playerRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, Input.GetAxisRaw("Vertical") * moveSpeed);
除非有其他问题。
可能有这么多条件,仅应用了4中的最后2个。
错误在于第二个“ if”语句中的代码。 我应该使用playerRigidbody.velocity.x而不是playerRigidbody.velocity.y。 Oopsies。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.