C# 为什么我的刚体2D没有';我不想正常地跳?

C# 为什么我的刚体2D没有';我不想正常地跳?,c#,unity3d,C#,Unity3d,我尝试使用Rigidbody2D.MovePosition使角色移动,但它仅适用于水平移动 我期望正常的真实跳跃,但当我移动角色时,它会随机跳跃,不会使用空格键跳跃 而且我的墙壁跳不起作用 预计角色将跳起来并水平移动到对面的墙。是的,但功率很小 我使用的是Unity 2019.4.16f using UnityEngine; public class D2Movement : MonoBehaviour { public byte moveSpeed; public byte plusJump

我尝试使用Rigidbody2D.MovePosition使角色移动,但它仅适用于水平移动

我期望正常的真实跳跃,但当我移动角色时,它会随机跳跃,不会使用空格键跳跃

而且我的墙壁跳不起作用

预计角色将跳起来并水平移动到对面的墙。是的,但功率很小

我使用的是Unity 2019.4.16f

using UnityEngine;

public class D2Movement : MonoBehaviour
{
public byte moveSpeed;
public byte plusJumpCount;
public float jumpStrength;
public byte wallJumpStrenght;
public bool isGrounded = false;
byte origJumpCount;
bool isWallL;
bool isWallR;
Rigidbody2D rb;

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

void Update(){
    Vector2 velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, 0f);
    if(Input.GetKeyDown(KeyCode.Space) && (plusJumpCount > 0 || isGrounded == true)){
        velocity.y = jumpStrength;
        plusJumpCount -= 1;
    }
    
    if(isGrounded == true){
        velocity.y = 0;
        plusJumpCount = origJumpCount;
    } else {
        velocity.y -= 10 * Time.deltaTime;
    }
    
    if(Input.GetKey(KeyCode.LeftShift)){
        rb.MovePosition((Vector2)transform.position + velocity * Time.deltaTime * 2);
    } else {
        rb.MovePosition((Vector2)transform.position + velocity * Time.deltaTime);
    }
}

void OnCollisionStay2D(Collision2D other){
    if(other.collider.tag == "Wall_L"){
        isWallL = true;
        if(Input.GetKeyDown(KeyCode.Space)){
            rb.AddForce(new Vector2(wallJumpStrenght, jumpStrength / 2), ForceMode2D.Impulse);
        }
    }
    
    if(other.collider.tag == "Wall_R"){
        isWallR = true;
        if(Input.GetKeyDown(KeyCode.Space)){
            rb.AddForce(new Vector2(-wallJumpStrenght, jumpStrength / 2), ForceMode2D.Impulse);
        }
    }
}

void OnCollisionExit2D(Collision2D other){
    isWallL = false;
    isWallR = false;
}
}
使用UnityEngine;
公共阶级运动:单一行为
{
公共字节速度;
公共字节plusJumpCount;
公众力量;
公共权力;
公共bool isGrounded=false;
字节起始计数;
布尔·伊斯瓦尔尔;
布尔·伊斯瓦勒;
刚体2d rb;
void Start()
{
rb=GetComponent();
}
无效更新(){
Vector2速度=新的Vector2(Input.GetAxis(“水平”)*moveSpeed,0f);
if(Input.GetKeyDown(KeyCode.Space)和&(plusJumpCount>0 | | isground==true)){
速度y=跳跃强度;
plusJumpCount-=1;
}
如果(isGrounded==真){
速度y=0;
plusJumpCount=origJumpCount;
}否则{
速度y-=10*Time.deltaTime;
}
if(Input.GetKey(KeyCode.LeftShift)){
rb.MovePosition((Vector2)transform.position+velocity*Time.deltaTime*2);
}否则{
rb.MovePosition((Vector2)transform.position+velocity*Time.deltaTime);
}
}
无效碰撞Stay2D(碰撞2D其他){
if(other.collider.tag==“Wall_L”){
isWallL=true;
if(Input.GetKeyDown(KeyCode.Space)){
rb.AddForce(新矢量2(墙壁跳跃强度,跳跃强度/2),ForceMode2D.Pulse);
}
}
if(other.collider.tag==“Wall_R”){
isWallR=真;
if(Input.GetKeyDown(KeyCode.Space)){
rb.AddForce(新矢量2(-WallJumpStrength,jumpStrength/2),ForceMode2D.Pulse);
}
}
}
空心OnCollisionExit2D(碰撞2D其他){
isWallL=false;
isWallR=假;
}
}

Pikramag频道,您是否为collision2D添加了相关组件,如Rigidbody2D和boxcollision?
只有我看到了你的编辑器代码,我不确定是否会出错。

我真的不明白你为什么会在这段代码中遇到这些错误,但是发生了一些不寻常的事情。也许试试

  • 使用内置的重力。可以在刚体组件中设置重力
  • 决定使用rb.MovePosition或rb.addForce跳跃
  • 保持代码简单-->没有真正使用ISWALL和ISWALL,请避免重复代码
  • 在FixeUpdate()中做物理相关的事情
  • 避免使用幻数(使用常量)
您将有更好的代码编写更少的错误


也可以直接考虑设置RB.速度。< / P>嗨!这更多的是一个评论,而不是对问题的回答。两行通常不够详细,因为原始海报可能不理解它们。你说的“相关”是什么意思?我有两个对撞机,一个是儿童,一个是RB2D。儿童碰撞器用于地面检测,可用于碰撞。RB2D对象中的碰撞器是主要的碰撞器