Unity3D 在给player添加水平移动时代码能正常运行不报错,player却没有水平移动,如何解决?

unity3D游戏制作过程中,手势输入识别和键盘输入识别的代码都成功运行且没有错误。但是在给player添加水平移动时代码能正常运行不报错,player却没有水平移动。
求帮忙看看!

img

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMove : View
{
    #region 常量
    #endregion

    #region 事件
    #endregion

    #region 字段

    public float speed = 20;
    //获取自身碰撞
    CharacterController m_cc;
    InputDirection m_inputDir = InputDirection.NULL;
    //是否按下鼠标
    bool activeInput = false;
    Vector3 m_mousePos;
    int m_nowIndex = 1;
    int m_targetIndex = 1;
    float m_xDistance;
    float m_moveSpeed = 13;

    #endregion

    #region 属性

    public override string Name { get { return Consts.V_PlayerMove; } }

    #endregion

    #region 方法
    //携程
    IEnumerator UpdataAction()
    {
        while (true)
        {
            m_cc.Move(transform.forward * speed * Time.deltaTime);
            MoveControl();
            UpdatePosition();
            
            
            yield return 0;
        }
    }

    //获取输入
    void GetInputDirection()
    {
        //手势识别
        m_inputDir = InputDirection.NULL;
        //如果按下鼠标左键
        if (Input.GetMouseButtonDown(0))
        {
            activeInput = true;
            m_mousePos = Input.mousePosition;
        }
        if (Input.GetMouseButton(0) && activeInput)
        {
            Vector3 Dir = Input.mousePosition - m_mousePos;
            if (Dir.magnitude > 20)
            {

                if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x > 0)
                {
                    m_inputDir = InputDirection.Right;
                }
                else if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x < 0)
                {
                    m_inputDir = InputDirection.Left;

                }
                else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y > 0)
                {
                    m_inputDir = InputDirection.UP;
                }

                else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y < 0)
                {
                    m_inputDir = InputDirection.Down;
                }
                activeInput = false;
            }
        }

        //键盘识别
        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space))
        {
            m_inputDir = InputDirection.UP;
        }
        else if(Input.GetKeyDown(KeyCode.S))
        {
            m_inputDir = InputDirection.Down;
        }
        else if (Input.GetKeyDown(KeyCode.A))
        {
            m_inputDir = InputDirection.Left;
        }
        else if (Input.GetKeyDown(KeyCode.D))
        {
            m_inputDir = InputDirection.Right;
        }

        print(m_inputDir);
    }

    //更新位置信息
    void UpdatePosition()
    {
        GetInputDirection();
        //判断位置信息
        switch (m_inputDir)
        {
            case InputDirection.NULL:
                break;
            case InputDirection.Right:
                if (m_targetIndex < 2)
                {
                    m_targetIndex ++;
                    m_xDistance = 2;                 
                }
                break;
            case InputDirection.Left:
                if (m_targetIndex > 0)
                {
                    m_targetIndex --;
                    m_xDistance = -2;
                }
                break;
            case InputDirection.Down:
                break;
            case InputDirection.UP:               
                break;
            default:
                break;
        }

    }

    //移动
    void MoveControl()
    {
        //左右移动
        if (m_targetIndex != m_nowIndex)
        {
            float move = Mathf.Lerp(0, m_xDistance, m_moveSpeed * Time.deltaTime);
            transform.position += new Vector3(move, 0, 0);
            m_xDistance -= move;
            if (Mathf.Abs(m_xDistance) < 0.05f)
            {
                m_xDistance = 0;
                m_nowIndex = m_targetIndex;

                switch (m_nowIndex)
                {
                    case 0:
                        transform.position = new Vector3(-2, transform.position.y, transform.position.z);                      
                        break;
                    case 1:
                        transform.position = new Vector3(0, transform.position.y, transform.position.z);
                        break;
                    case 2:
                        transform.position = new Vector3(2, transform.position.y, transform.position.z);
                        break;

                }
            }
        }

    }

    #endregion

    #region Unity回调

    private void Awake()
    {
        m_cc = GetComponent<CharacterController>();
    }

    private void Start()
    {
        //开启携程
        StartCoroutine(UpdataAction());
    }


    #endregion

    #region 事件回调

    public override void HandEvent(string name, object data)
    {
        
    }


    #endregion

    #region 帮助方法
    #endregion
}

  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:【Unity破事水】如何实现线上试卷选择题的选项
  • 除此之外, 这篇博客: 微信小程序图片添加水印并等比列缩放中的 最后附上本人做的一个小程序 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •    

  • 以下回答由chatgpt基于相关博客总结生成:

    抱歉,参考资料没有提供与“添加水平移动”的问题相关的代码或信息,因此无法提供具体的解决方案。另外,也没有提供足够的上下文来确定“player”的具体指代,因此也无法确定控制方式。建议提供更多上下文或信息,以便给出更准确的回答。

打个断点,或者写print,看update函数到底执行了没有
目测是根本没执行
原始的update方法被你弄哪去了,为什么不写在update里面