0

我正在创建一个马里奥克隆,我需要保持玩家的前进动力进入跳跃,然后继续进入着陆。我不知道如何实现这一目标。每次我着陆时,玩家都必须再次建立动力。任何想法如何解决这个问题?我尝试了几种解决方案均无济于事。我认为这与我在向左或向右时如何向玩家增加力量和加速度有关。不确定是否有任何帮助将不胜感激。提前致谢。

这是我的代码:

Animator animator;
Rigidbody2D rb;
bool isGrounded;
public float moveSpeed;
public Vector2 acceleration;
public float jumpHeight;
public float lowjumpMultiplier;
public Transform groundCheckM;
public Transform groundCheckL;
public Transform groundCheckR;
public float storedValue;

void Start()
{
    animator = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();
    transform.eulerAngles = new Vector3(0, 0, 0);
}


private void Update()
{

   if
         ((Physics2D.Linecast(transform.position,groundCheckM.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))) || //Check if grounded
         (Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))) ||
         (Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))))
    {
        isGrounded = true;
        animator.SetBool("Jump", false);
    }
       
   else
    {
        isGrounded = false;
    } 

    animator.SetFloat("Walk", rb.velocity.x);  //Set animation float to x velocity 
   
    if (rb.velocity.x <= 0.03f && rb.velocity.x >= -0.03f && isGrounded) //Play "Idle" animation 
    {
        animator.Play("Mario_Idle");
    }

    if (rb.velocity.x >=4 || rb.velocity.x <=-4)

    {
        animator.speed = Mathf.Abs(rb.velocity.x / 5.5f); //Increase speed of walking animation with player's walking speed 
    }

}
void FixedUpdate()
{

    if (Input.GetKey("d") || Input.GetKey("right")) //Move player to the right 
    {
        rb.AddForce(acceleration * rb.mass);
        transform.rotation = Quaternion.Euler (0, 0, 0);
    }

    else if (Input.GetKey("a") || Input.GetKey("left")) //Move player to the left
    {
        rb.AddForce(-acceleration * rb.mass);
        transform.rotation = Quaternion.Euler(0, 180, 0);
    }

        if (rb.velocity.x >= 10)
        {
            rb.velocity = new Vector2(10, rb.velocity.y);   //Cap player speed at 10 when moving right
        }

    else if (rb.velocity.x <= -10)
        {
            rb.velocity = new Vector2(-10, rb.velocity.y);  //Cap player speed at 10 when moving left 
    }


    if (Input.GetKey("space") && isGrounded) //Player jump 
    {
        rb.velocity += new Vector2(rb.velocity.x, jumpHeight);
        animator.SetBool("Jump", true);
    }

}

}

4

2 回答 2

1

我觉得很愚蠢,但答案在物理材料中。一旦我降低了摩擦力,它就会让跳跃的动量带入玩家的跑步速度。我想这是一个很好的提醒,直接在 Unity 及其内置的物理系统中进行修补。

于 2021-04-20T01:03:42.860 回答
0

如果您想在跳跃时保持动力,您可以将其存储在一个变量中,该变量会增加直到达到最大值,或者您松开按键。

float acceleration;
float accelFactor = 0.6f;
float deAccelFactor = 1f;

bool jumping; //you should set it to true when jumping and false, when not.
Rigidbody2D rb;

void Start(){
    rb = GetComponent<Rigidbody2D>();
}
void Update(){
    if (Input.GetKeyDown(KeyCode.D))
    {
        Accelerate();
        rb.AddForce(acceleration * rb.Mass);
    }
    else if (jumping)
    {
        rb.AddForce(acceleration * rb.Mass);
    }
    else
    {
        DeAccel();
    }
}
void Accelerate(){
    acceleration += accelFactor * Time.deltaTime;
    acceleration = Mathf.Clamp(acceleration, 0, maxAccel);
}
void DeAccel(){
    acceleration -= deAccelFactor * Time.deltaTime;
    acceleration = Mathf.Clamp(acceleration, 0, maxAccel);
}

这是我建议使用的,也就是说,如果我理解你的意思。

于 2021-04-19T22:40:03.197 回答