【问题标题】:How to enable / disable player movement in unity?如何统一启用/禁用玩家移动?
【发布时间】:2020-06-15 07:27:52
【问题描述】:

所以,我正在制作一个游戏,其中一个玩家通过按下 shift 来改变对两个角色的控制,所以,当按下 shift 时,玩家 1 的移动被禁用并且玩家 2 脚本被启用,我通过使移动速度和跳跃达到数字为零,但是当按下shift时没有任何反应,我该怎么做才能解决这个问题?

顺便说一下,我将Fire2设置为“shift”以澄清,并且还关注“CanPlay”变量,因为这是我认为问题所在。

代码:

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

public class movimento : MonoBehaviour
{

    public bool CanPlay;

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float speedY = PlayerRB.velocity.y;

        PlayerRB.velocity = new Vector2(h * speed, speedY);

        if (Input.GetButtonDown("Jump") && Isgrounded == true)
        {
            PlayerRB.AddForce(new Vector2(0, jumpforce));
        }

        if (h > 0 && IsLookLeft == true)
        {
            Flip();
        }
        else if (h < 0 && IsLookLeft == false)
        {
            Flip();
        }

        if (CanPlay = false)
        {
            speed = 0f;
            jumpforce = 0f;
        } 

        if (CanPlay = true)
        {
            speed = 10f;
            jumpforce = 150f;
        }

        if (Input.GetKeyDown("Fire2"))
        {
            CanPlay = !CanPlay;
        }
    }
}


【问题讨论】:

    标签: c# unity3d 2d-games


    【解决方案1】:

    你的 if 有一个错误。应该是:

    if (CanPlay == false)
        {
            speed = 0f;
            jumpforce = 0f;
        }
    
        if (CanPlay == true)
        {
            speed = 10f;
            jumpforce = 150f;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-20
      • 2023-01-02
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多