Loops 如何在c#中重复一个动作来修复跳跃机制

Loops 如何在c#中重复一个动作来修复跳跃机制,loops,unity3d,visual-studio-2017,Loops,Unity3d,Visual Studio 2017,我正在创建一个基本的2D platformer游戏,但是跳跃机制只运行一次,然后直接降落。我如何循环这个 我尝试了检测碰撞(来自tag:Terrain),这确实有很大帮助,但仍然无法正常工作 这是我的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController2D : MonoBehaviour { private Rigi

我正在创建一个基本的2D platformer游戏,但是跳跃机制只运行一次,然后直接降落。我如何循环这个

我尝试了检测碰撞(来自tag:Terrain),这确实有很大帮助,但仍然无法正常工作

这是我的代码:

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

public class PlayerController2D : MonoBehaviour
{
    private Rigidbody2D rb2D;

    private Vector2 velocity;

    Vector2 xvelocity = new Vector2(10, 0);
    Vector2 yvelocity = new Vector2(0, 10);

    public float jumptime;
    bool grounded = true;

    bool jump = false;

    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Input.GetButton("Jump"))
        {
            jump = true;
        }

    }

    private void FixedUpdate()
    {
        //Debug.Log(Input.GetAxisRaw("Vertical"));
        if (left == true)
        {
            //Debug.Log("Left");
            rb2D.MovePosition(rb2D.position + -xvelocity * Time.fixedDeltaTime);
        }

        if (right == true)
        {
            //Debug.Log("Right"); 
            rb2D.MovePosition(rb2D.position + xvelocity * Time.fixedDeltaTime);
        }

        //if (jump == true && grounded == true)
        //{
        //    jumptime -= Time.fixedDeltaTime;
        //    if (jumptime > 0)
        //    {
        //        rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
        //    }
        //    if (jumptime <= 0)
        //    {
        //        jump = false;
        //        jumptime = 2;
        //    }

        if (jump == true && grounded == true && jumptime > 0)
        {
            jumptime -= Time.fixedDeltaTime;
            rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
        } else if (jumptime <= 0)
        {
            jump = false;
            jumptime = 2f;
        }

        //if (Time.fixedDeltaTime >= 2)
        //{
        //    jump = false;
        //    rb2D.MovePosition(rb2D.position + -yvelocity * Time.fixedDeltaTime);
        //}
    }

    void OnCollisionExit2D(Collision2D other)
    {
        Debug.Log("No longer in contact with " + other.transform.name);
        jump = true;
        grounded = false;
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Terrain")
        {
            Debug.Log("Landed");
            jump = false;
            grounded = true;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PlayerController 2D:单行为
{
私有刚体2d rb2D;
专用矢量2速度;
矢量2 x速度=新矢量2(10,0);
向量2 yvelocity=新向量2(0,10);
公共时间;
bool-grounded=true;
布尔跳跃=假;
void Start()
{
rb2D=GetComponent();
}
无效更新()
{
if(Input.GetButton(“跳转”))
{
跳跃=真;
}
}
私有void FixedUpdate()
{
//Log(Input.GetAxisRaw(“垂直”);
如果(左==真)
{
//Debug.Log(“左”);
rb2D.MovePosition(rb2D.position+-xvelocity*Time.fixedDelatime);
}
if(right==true)
{
//Debug.Log(“右”);
rb2D.MovePosition(rb2D.position+xvelocity*时间固定时间);
}
//如果(跳转==true&&grounded==true)
//{
//jumptime-=Time.fixedDelatime;
//如果(跳转时间>0)
//    {
//rb2D.MovePosition(rb2D.position+yvelocity*Time.FixedDelatime);
//    }
//如果(跳转时间0)
{
jumptime-=Time.fixedDelatime;
rb2D.MovePosition(rb2D.position+yvelocity*Time.FixedDelatime);
}否则如果(跳时=2)
//{
//跳跃=假;
//rb2D.MovePosition(rb2D.position+-yvelocity*Time.fixedDelatime);
//}
}
空心OnCollisionExit2D(碰撞2D其他)
{
Log(“不再与“+other.transform.name”联系);
跳跃=真;
接地=假;
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.gameObject.tag==“地形”)
{
调试日志(“登陆”);
跳跃=假;
接地=正确;
}
}
}

预期结果是动作“跳跃”将以良好的高度(矢量2 yvelocity)循环约1/1.5秒,因此它会跳得更高,然后会下降(由于刚体的重力(2D))

如评论中所述,我认为主要问题来自这一行代码

if (jump == true && grounded == true && jumptime > 0)
很可能其中的一个bool不是您所期望的。无论如何,我建议您将该行转换为:

if (jump && grounded && jumptime > 0)
对于布尔型,不需要==true

无论如何,为了更容易地解决您的问题,我建议您使用AddForce而不是move(因为您使用的是rigibody2d)

关于水平速度的小提示。如果使用刚体,最好使用相同的刚体移动它,而不是使用变换:

rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);
您的代码将成为:

公共类PlayerController 2D:单行为
{
私有刚体2d rb2D;
专用矢量2速度;
公众浮力=5;
bool-grounded=true;
void Start(){rb2D=GetComponent();}
无效更新()
{
if(Input.GetButton(“跳转”)和接地)
rb2D.AddForce(矢量2.up*跳跃力,ForceMode2D.Pulse);
//在这里计算水平速度
xspeed=。。。;
}
私有void FixedUpdate()
{
rb2D.MovePosition(rb2D.position+Vector2.left*xspeed*Time.fixedDelatime);
}
空心OnCollisionExit2D(碰撞2D其他)
{
Log(“不再与“+other.transform.name”联系);
接地=假;
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.gameObject.tag==“地形”)
{
调试日志(“登陆”);
接地=正确;
}
}
}

查看您的代码,找出
时发生的情况(jump==true&&grounded==false&&jumptime>0)
嘿,谢谢,我检查了代码,它工作了,但是字符在空中移动时是浮动的。编辑:
transform.position-=Vector3.left*xspeed*Time.deltaTime;
修复了它。这很可能取决于你如何处理水平移动(我看不到问题中的代码)。如果玩家不在地面上,可能你没有检查水平速度。无论如何,我将其添加到代码中。最后一个不是,最好使用相同的刚体轴(也用于水平移动)移动刚体,而不是使用变换。答案中的代码中添加了所有注释。我使用了
rb2D.AddForce(Vector2.up*jumpForce,ForceMode2D.Pulse);
用于跳跃和
transform.position+=Vector3.left*xspeed*Time.deltaTime;
用于左侧和右侧,效果完美。
rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);