C# 我想让它成为这样,当我在空中用鼠标右键跳跃一次时,我可以';不要再这样做了

C# 我想让它成为这样,当我在空中用鼠标右键跳跃一次时,我可以';不要再这样做了,c#,unity3d,C#,Unity3d,所以我有一个问题,我想让它成为这样,当我在空中右击一次(作为额外的跳跃)(用空格跳跃后),我不能再做了 以下是我当前的移动脚本: public class Move2D : MonoBehaviour { public float moveSpeed = 5f; public bool isGrounded = false; void Update() { Jump(); Vector3 movement = new Vecto

所以我有一个问题,我想让它成为这样,当我在空中右击一次(作为额外的跳跃)(用空格跳跃后),我不能再做了

以下是我当前的移动脚本:

public class Move2D : MonoBehaviour 
{
    public float moveSpeed = 5f;
    public bool isGrounded = false;

    void Update()
    {
        Jump();
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position += movement * Time.deltaTime * moveSpeed;
    }

    void Jump()
    {
        if (Input.GetButtonDown("Jump") && isGrounded == true)
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 8f), ForceMode2D.Impulse);
        }

        if (Input.GetMouseButtonDown(1) && isGrounded == false)
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 4f), ForceMode2D.Impulse);
        }
    }
}
public类Move2D:monobhavior
{
公共浮子移动速度=5f;
公共bool isGrounded=false;
无效更新()
{
跳跃();
Vector3移动=新的Vector3(Input.GetAxis(“水平”),0f,0f;
transform.position+=移动*时间.延迟*移动速度;
}
无效跳转()
{
if(Input.GetButtonDown(“Jump”)&&isground==true)
{
gameObject.GetComponent().AddForce(新矢量2(0f,8f),ForceMode2D.pulse);
}
if(Input.GetMouseButtonDown(1)和&isground==false)
{
gameObject.GetComponent().AddForce(新矢量2(0f,4f),ForceMode2D.pulse);
}
}
}
这是我目前的“固定”脚本

公共类:单一行为
{
游戏对象玩家;
//在第一帧更新之前调用Start
void Start()
{
Player=gameObject.transform.parent.gameObject;
}
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
if(collision.collider.tag==“地面”)
{
Player.GetComponent().isground=true;
}
}
私有空心OnCollisionExit2D(碰撞2D碰撞)
{
if(collision.collider.tag==“地面”)
{
Player.GetComponent().isground=false;
}
}
}

我在youtube上遵循了这个脚本。请回答,如果我需要提供更多的信息,为今后的职位。我对编码很陌生

所以你已经有了
isground
来防止你的跳转键被双击(我想你说过空格),而且这个逻辑足够简单,可以扩展到工作中来防止双重提升:

  • 添加新的全局变量
    isBoosted
    (将其添加到
    isground
    下)
  • 当你跳跃时,除了给你的怪物增加一些动力外,还将
    isboost
    设置为false
  • 右键单击进行第二次跳转时,请检查
    isboost
    是否为false
  • 在右键单击的代码中,将
    isboost
    设置为true,并为模型添加更多动力
第一次提升时,您会将
isboost
设置为true,第二次尝试提升时将失败,因为
isboost
为false的测试将失败。当你撞到地面,并再次使用空间跳跃时,它将重置,因此每次“重置”时,你都可以获得新的助力


这是最好的方法吗?可能不会,但它与您已有的一致…

首先始终存储引用,以避免做不必要的工作

然后我会反过来说:

  • 固定脚本只知道它是否固定

  • Move2D主动请求该值,但不会“神奇地”从其他地方设置该值

在你的脚踏实地的剧本中,我只有

public class Grounded : MonoBehaviour
{
    // Read-only public property
    public bool IsGrounded { get; private set; }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        // Note: Rather use CompareTag instead of == !
        // it is not only faster but also secure against typos. 
        // == would fail silently while CompareTag throws an exception 
        // indicating that you passed an invalid tag
        if (collision.collider.CompareTag("Ground"))
        {
            IsGrounded = true;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Ground"))
        {
            IsGrounded = false;
        }
    }
}
然后,如果我理解正确,您可以添加一个简单的bool标志
CanDoubleJump
,该标志在跳转后仅可用一次,并在使用后重置为false。所以Move2D看起来像

public class Move2D : MonoBehaviour
{
    // Already reference this via drag&drop in the Inspector
    [SerializeField] private Rigidbody2D _rigidbody;

    // I would also go this way round and let the Move2D script
    // actively check for the value instead of getting it set from somewhere else
    [SerializeField] private Grounded _grounded;

    // As fallback get it once on runtime
    private void Awake ()
    {
        if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
        if(!_grounded) _grounded = GetComponentInChildren<Grounded>(true);
    }


    private bool CanDoubleJump;

    private void Update()
    {
        Jump();
        // Note instead of going through the transform which breaks the Physics
        // I would rather simply set the velocity and let the Rigidbody itself handle the rest
        _rigidbody.velocity = new Vector2(Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed, _rigidbody.velocity.y);
    }

    private void Jump()
    {
        // Checking the bool flag is cheaper so do it first
        if (_grounded.IsGrounded && Input.GetButtonDown("Jump"))
        {
            _rigidbody.AddForce(Vector2.up * 8f, ForceMode2D.Impulse);
            CanDoubleJump = true;
        }
        // I would add else since anyway both can't be true at the same time
        else if (CanDoubleJump && !_grounded.IsGrounded && Input.GetMouseButtonDown(1))
        {
            _rigidbody.AddForce(Vector2.up * 4f, ForceMode2D.Impulse);
            CanDoubleJump = false;
        }
        // Reset the flag if it wasn't used just in case
        else if(_grounded.IsGrounded)
        {
            CanDoubleJump = false;
        }
    }
}
public类Move2D:monobhavior
{
//已在Inspector中通过拖放引用此
[SerializeField]私有刚体2d_刚体;
//我也会这样做,让Move2D脚本
//主动检查值,而不是从其他地方设置
[SerializeField]专用接地\u接地;
//作为后备,在运行时获取一次
私人空间()
{
如果(!\u rigidbody)\u rigidbody=GetComponent();
如果(!\u接地)\u接地=GetComponentChildren(真);
}
私人跳伞;
私有void更新()
{
跳跃();
//注意,而不是通过破坏物理的变换
//我宁愿简单地设置速度,让刚体自己来处理其余的
_rigidbody.velocity=新矢量2(Input.GetAxis(“水平”)*Time.deltaTime*moveSpeed,_rigidbody.velocity.y);
}
私有无效跳转()
{
//检查bool标志比较便宜,所以先检查
如果(_ground.isground&&Input.GetButtonDown(“跳转”))
{
_刚体.附加力(矢量2.up*8f,力模2D.脉冲);
CanDoubleJump=真;
}
//我还想补充一点,因为无论如何,这两者不可能同时成立
else if(CanDoubleJump&&!\u grounded.IsGrounded&&Input.GetMouseButtonDown(1))
{
_刚体.附加力(矢量2.up*4f,力模2D.脉冲);
CanDoubleJump=假;
}
//如果不是为了以防万一而使用,请重置标志
否则,如果(_ground.isground)
{
CanDoubleJump=假;
}
}
}


在智能手机上输入,但我希望想法变得清晰

欢迎使用SO!为了让其他人更容易回答这个问题,你能用正确的上下文标记它吗?例如,这看起来像是Unity,但我不确定。另外,请查看谢谢!我以后一定要做:)非常感谢!我会试试的。)谢谢你:D那帮了大忙。
public class Move2D : MonoBehaviour
{
    // Already reference this via drag&drop in the Inspector
    [SerializeField] private Rigidbody2D _rigidbody;

    // I would also go this way round and let the Move2D script
    // actively check for the value instead of getting it set from somewhere else
    [SerializeField] private Grounded _grounded;

    // As fallback get it once on runtime
    private void Awake ()
    {
        if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
        if(!_grounded) _grounded = GetComponentInChildren<Grounded>(true);
    }


    private bool CanDoubleJump;

    private void Update()
    {
        Jump();
        // Note instead of going through the transform which breaks the Physics
        // I would rather simply set the velocity and let the Rigidbody itself handle the rest
        _rigidbody.velocity = new Vector2(Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed, _rigidbody.velocity.y);
    }

    private void Jump()
    {
        // Checking the bool flag is cheaper so do it first
        if (_grounded.IsGrounded && Input.GetButtonDown("Jump"))
        {
            _rigidbody.AddForce(Vector2.up * 8f, ForceMode2D.Impulse);
            CanDoubleJump = true;
        }
        // I would add else since anyway both can't be true at the same time
        else if (CanDoubleJump && !_grounded.IsGrounded && Input.GetMouseButtonDown(1))
        {
            _rigidbody.AddForce(Vector2.up * 4f, ForceMode2D.Impulse);
            CanDoubleJump = false;
        }
        // Reset the flag if it wasn't used just in case
        else if(_grounded.IsGrounded)
        {
            CanDoubleJump = false;
        }
    }
}