Question by
joni_ahvenjarvi · Feb 26, 2020 at 07:50 AM ·
2d-platformer2d-physicsjumpingarc
Trying to make 2D jumping arc
So, we're making a 2D platformer at school and i'm trying to make it so the player has a proper jumping arc instead of falling down like a wet rag when you let go of the movement key during a jump. Here's our movement code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerPlatformerController : PhysicsObject
{
public float maxSpeed = 7;
public float jumpTakeOffSpeed = 7;
private SpriteRenderer spriteRenderer;
private Animator animator;
// Use this for initialization
void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
protected override void ComputeVelocity()
{
Vector2 move = Vector2.zero;
move.x = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && grounded)
{
velocity.y = jumpTakeOffSpeed;
}
else if (Input.GetButtonUp("Jump"))
{
if (velocity.y > 0)
{
velocity.y = velocity.y * 0.5f;
}
}
bool flipSprite = (spriteRenderer.flipX ? (move.x > 0f) : (move.x < 0f));
if (flipSprite)
{
spriteRenderer.flipX = !spriteRenderer.flipX;
}
animator.SetBool("grounded", grounded);
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
targetVelocity = move * maxSpeed;
}
}
But currently the player just falls straight down after a short while if you let go of the movement key... I want them to continue to move, like in Mario etc.
Help would be most appreciated!
Comment
Your answer
Follow this Question
Related Questions
Can't control jumping while moving horizontally 0 Answers
Where is the Jump Input in the default 2d platform controller? 0 Answers
Broken Jump Physics 0 Answers
Player can only jump once!? 0 Answers