unity3d 我如何使用新的输入系统动画我的球员?

k4emjkb1  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(166)

我在这里使用Brakeys动画指南:Movement thing,这是在旧的输入系统中设置的。我正在制作一个移动的游戏,因此使用新的输入系统,但行“horizontalMove = Input.GetAxis(“Horizontal”)* moveSpeed;“不管用
当我加载到游戏中时,我得到这个错误消息:'Assets\PlayerMovement.cs(46,46):错误CS 0103:当前上下文中不存在名称“horizontalMove”,将弹出“”。
代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    public Animator animator;
    
    [Header("Movement Variables")]
    private Rigidbody2D rb;
    private Vector2 moveVector;
    public float jumpDistance;
    public float moveSpeed;
    [SerializeField] bool isFacingRight;

    [Header("Ground Check")]
    [SerializeField] bool isGrounded;
    public Transform groundSpot;
    public LayerMask groundLayer;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        isFacingRight = true;
    }

    public void Move(InputAction.CallbackContext context)
    {
        moveVector = context.ReadValue<Vector2>();

    }

    public void Jump(InputAction.CallbackContext context)
    {
        if(context.performed && isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpDistance);
        }
    }
    private void Update()
    {
        horizontalMove =  Input.GetAxis("Horizontal") * moveSpeed;
        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
        isGrounded = Physics2D.OverlapCircle(groundSpot.position, 0.1f, groundLayer);
        CheckDirection();
    }

    private void FixedUpdate()
    {

        rb.velocity = new Vector2(moveVector.x * moveSpeed, rb.velocity.y);

    }

    void CheckDirection()
    {
        if(moveVector.x > 0 && !isFacingRight)
        {
            Flip();
        }
        else if(moveVector.x < 0 && isFacingRight)
        {
            Flip();
        }
    }   

    void Flip()
    {
        isFacingRight = !isFacingRight;
        transform.Rotate(new Vector3(0, 180, 0));
    }

}

请帮帮忙
我试着在脚本中把它改成其他东西,比如rb.velovity和'Move',但是它们都出现了同样的问题

nr7wwzry

nr7wwzry1#

我发现了问题。就像hijinxbassist在他的评论中说的,变量horizontalMove没有在任何地方声明。我用moveVector.x替换了它。我还删除了horizontalMove = Input.GetAxis("Horizontal") * moveSpeed;,因为它与Move()冲突

相关问题