【问题标题】:Unity2D - Stop movement direction once user input returns to zero but keep same direction facingUnity2D - 一旦用户输入返回零但保持相同方向朝向,则停止移动方向
【发布时间】:2023-01-12 11:15:36
【问题描述】:

我已经忍无可忍了,我正在学习并尝试创建一个简单的角色控制器。我的动画设置正确,但我的脚本似乎让我的播放器朝最后已知的输入方向移动。

一旦用户输入返回到 0,我希望我的播放器停止一起移动,但继续面向最后一个已知方向,即向上、向下、向右或向左。只需在最后一次定向输入时冻结该动画帧即可。

但他只是一直走着。有谁可以帮助我吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5.0f;
private Vector2 moveDirection;
private Vector2 lastMoveDirection;
private bool playerMoving;
private Rigidbody2D rb;
private Animator anim;
private static bool playerExists;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();

    if (!playerExists)
    {
        playerExists = true;
        DontDestroyOnLoad(transform.gameObject);
    }
    else
    {
        Destroy(gameObject);
    }

}

void Update()
{
    moveDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    // Mobile controls
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        if (touch.position.x < Screen.width / 2)
            moveDirection.x = -1;
        else
            moveDirection.x = 1;

        if (touch.position.y < Screen.height / 2)
            moveDirection.y = -1;
        else
            moveDirection.y = 1;
    }
    // Limit the movement to only horizontal or vertical


    if (Mathf.Abs(moveDirection.x) > Mathf.Abs(moveDirection.y))
        moveDirection.y = 0;
    else
        moveDirection.x = 0;

    // replace moveDirection with lastMoveDirection when input is zero
    if (moveDirection == Vector2.zero) {
        moveDirection = lastMoveDirection;
    } else {
        lastMoveDirection = moveDirection;
    }

    anim.SetFloat("MoveX", moveDirection.x);
    anim.SetFloat("MoveY", moveDirection.y);
    anim.SetBool("PlayerMoving", moveDirection != Vector2.zero);

}

void FixedUpdate()
{
    rb.MovePosition(rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime);
}
}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    Opps 解决了..感谢那些看过的人。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class PlayerController : MonoBehaviour
    {
        public float moveSpeed = 5.0f;
        private Vector2 moveDirection;
        private Vector2 lastMoveDirection;
        private bool playerMoving;
        private Rigidbody2D rb;
        private Animator anim;
        private static bool playerExists;
    
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            anim = GetComponent<Animator>();
    
            if (!playerExists)
            {
                playerExists = true;
                DontDestroyOnLoad(transform.gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
    
        }
    
        void Update()
        {
            moveDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
            // Mobile controls
            if (Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);
                if (touch.position.x < Screen.width / 2)
                    moveDirection.x = -1;
                else
                    moveDirection.x = 1;
    
                if (touch.position.y < Screen.height / 2)
                    moveDirection.y = -1;
                else
                    moveDirection.y = 1;
            }
            // Limit the movement to only horizontal or vertical
    
    
            if (Mathf.Abs(moveDirection.x) > Mathf.Abs(moveDirection.y))
                moveDirection.y = 0;
            else
                moveDirection.x = 0;
            
            if (moveDirection == Vector2.zero) {
                moveDirection = lastMoveDirection;
                moveDirection = Vector2.zero;
            } else {
                lastMoveDirection = moveDirection;
            }
    
            anim.SetFloat("MoveX", lastMoveDirection.x);
            anim.SetFloat("MoveY", lastMoveDirection.y);
            anim.SetBool("PlayerMoving", moveDirection != Vector2.zero);
    
        }
    
        void FixedUpdate()
        {
            rb.MovePosition(rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多