英文:
Better jumping in unity 2d?
问题
我目前正在开发一个2D游戏,用于跳跃我正在使用Rigidbody.AddForce()
。然而,这导致我的角色在落地时下落速度较慢。我想要使其快速着陆。
我尝试过操控重力比例和质量的数值,但这会影响到角色的移动,并且跳跃效果也不理想。
以下是你的代码部分:
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && PlayerCanMove == true)
{
if (CheckGrounded() == true && OnSlope() == false)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else if(CheckGrounded() == true && OnSlope() == true)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else
{
IsJumping = false;
Catanim.SetBool("isJumping", false);
}
}
}
这里是Rigidbody2D组件的图像: 这里
英文:
I am currently working on a 2d game, and for jumping, I'm using Rigidbody.Addforce()
. However, this causes my player to land back to the ground slowly. I want it to land back quickly.
I tried manipulating the values of Gravity scale and mass, but that causes effects over player movement and again that jump doesn't come out well.
Here's the code:-
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && PlayerCanMove == true) //Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended
{
if (CheckGrounded() == true && OnSlope() == false)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else if(CheckGrounded() == true && OnSlope() == true)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else
{
IsJumping = false;
Catanim.SetBool("isJumping", false);
}
}
}
}
Here's an image of the rigidbody2d component:-
here
答案1
得分: 0
我建议使用一个角色控制器,并且如果你想制作一个2D游戏,可以自定义重力。然而,如果你想继续使用刚体并且想要调整重力,你可以这样做:
public float gMoon = 1.6f; // 月球上的重力.
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(new Vector3(0, -1.0f, 0) * rb.mass * gMoon);
}
英文:
I'd recommend using a character controller and making your gravity if you are trying to make a 2D game. However, if you want to stick to rigid body and want to adjust your gravity your can do that:
public float gMoon=1.6f; //gravity on the moon.
void Start()
{
rb=GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(new Vector3(0, -1.0f, 0)*rb.mass*gMoon);
}
答案2
得分: 0
In short, to make a better jump, you need a condition to determine the up and down movement. The code below is the key to solving the problem.
if (RBody.velocity.y > 0) // moving UpSide
{
RBody.gravityScale = 1;
}
else // moving on ground
{
RBody.gravityScale = 2; // double gravity scale for jumping down
}
And to summarize the code:
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && ...)
{
...
}
RBody.gravityScale = RBody.velocity.y > 0 ? 1f : 2f; // define upside gravity first and down side gravity second
}
英文:
In short, to make a better jump, you need a condition to determine the up and down movement. The code below is the key to solving the problem.
if (RBody.velocity.y > 0) // moving UpSide
{
RBody.gravityScale = 1;
}
else // moving on ground
{
RBody.gravityScale = 2; // double gravity scale for jumping down
}
And to summarize the code:
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && ...)
{
...
}
RBody.gravityScale = RBody.velocity.y > 0 ? 1f : 2f; // define upside gravity first and down side gravity second
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论