C# 如何解决nojump对象的问题,即必须跳转?

C# 如何解决nojump对象的问题,即必须跳转?,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我的代码有问题,我已经开始学习C#,所以我试着编写一些游戏(比如几何短跑)。我想要那个移动和点击跳跃。我写了“移动代码”和“onclick跳转代码”,但不起作用 我在微软Visual Studio中用C#写作,在Unity 2018.3.5f1中设计 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Plaxercontrol2 : MonoBehaviour

我的代码有问题,我已经开始学习C#,所以我试着编写一些游戏(比如几何短跑)。我想要那个移动和点击跳跃。我写了“移动代码”和“onclick跳转代码”,但不起作用

我在微软Visual Studio中用C#写作,在Unity 2018.3.5f1中设计

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

public class Plaxercontrol2 : MonoBehaviour
{
public Rigidbody2D rb;
public Transform groundCheck;
public Transform startPosition;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update()
{
    rb.velocity = new Vector2(3, rb.velocity.y);
    onGround = Physics2D.OverlapCircle(groundCheck.position, 
groundCheckRadius, whatIsGround);

    if (Input.GetMouseButtonDown(0) && onGround)
    {
        rb.velocity = new Vector2(5, rb.velocity.x);
    }

}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类控制2:单一行为
{
公共刚体2d rb;
公开审查;
公共转换起始位置;
公众浮标地面检查半径;
公共层码头;
私人住宅;
//在第一帧更新之前调用Start
void Start()
{
rb=GetComponent();
}
//每帧调用一次更新
无效更新()
{
rb.velocity=新矢量2(3,rb.velocity.y);
onGround=物理2D.重叠圆(地面检查位置,
地面检查半径,whatIsGround);
if(Input.GetMouseButtonDown(0)和&onGround)
{
rb.velocity=新矢量2(5,rb.velocity.x);
}
}
}
现在,它只是在移动

和团结的画面

我确信你应该使用
rb.AddForce()
或类似的工具,而不是改变你自己的速度。这就是移动刚体的方式

你可以这样做:

rb.AddForce(Vector2.up * whatEverValue, ForceMode2D.Impulse)

什么不起作用?如果您有任何错误,请显示。你尝试了什么价值观?什么价值观?试试1,10,100,1000。添加力取决于刚体的质量,所以您可以调整值以获得所需的力。您确定执行了吗?尝试在rb.AddForce()之前添加一个Debug.Log(),看看它是否在那里,因为它应该可以工作,以及如何添加Debug.Log()?我很抱歉(Input.GetMouseButtonDown(0)和&onGround){Debug.Log(“我们添加了力量!”);rb.velocity=newvector2(5,rb.velocity.x);}它什么也不做,但我注意到Unity中的警告:UnassignedReferenceException:Plaxercontrol2的变量groundCheck尚未分配。您可能需要在inspector中指定Plaxercontrol2脚本的groundCheck变量。