如何统一启用/禁用玩家移动?

问题描述 投票:0回答:1

因此,我正在制作一个游戏,其中一个玩家通过按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个回答
1
投票

您的if错误。应该是:

© www.soinside.com 2019 - 2024. All rights reserved.