- Home /
Diagonal jump results with weird motion.
What i want to achieve is the player being able to jump diagonally. using this code kinda works however player jumps more to the y-axis. instead of going x,y(15,15) it jumps more like x,y(5,15) even though xpower is same as the ypower. and if i increase the xpower to 30 or more. it starts off by going left or right incredibly fast for a brief second before even starting to go upwards.
GetAxis("Horizontal_LRUD") will return between -1,1 depending on the left right input.
private Rigidbody2D rg2d;
void Start()
{
rg2d = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetAxis("Horizontal_LRUD") != 0f)
{
rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * 5, rg2d.velocity.y);
}
if (Input.GetKeyDown(KeyCode.A))
{
int xpower = 15, ypower = 15;
rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * xpower, ypower);
}
}
Answer by lgarczyn · Dec 19, 2019 at 06:46 PM
It's pretty simple, you erase the previous horizontal velocity every frame, therefore your horizontal jump only works for one frame.
To fix this, you need to check if you are currently in the air, and if so, keep the previous horizontal velocity.
The problem will be a lack of control mid-air, which is usually not user-friendly. You can reduce that by instead using mid-air acceleration. Basically instead of replacing the previous velocity, you increment it every frame by the current input.
velocity += Input.GetAxisRaw("Horizontal_LRUD") * airAcceleration * Time.deltaTime;
velocity.x = Mathf.Clamp(rg2d.velocity.x, -maxAirSpeed, maxAirSpeed);
Please note that because this is in Update, you will get slightly different results depending on Framerate.
Thank you, i solved it by doing this if you have any suggestions please let me know!
void FixedUpdate()
{
if (footed)
{
if (Input.GetAxis("Horizontal_LRUD") != 0)
rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * 5, rg2d.velocity.y);
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
int xpower = 10, ypower = 10;
footed = false;
rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * xpower, ypower);
}
else if (Input.GetAxis("Vertical_LRUD") > 0)
{
footed = false;
rg2d.velocity = new Vector2(rg2d.velocity.x, 6);
}
}
else
{
if (Input.GetAxis("Horizontal_LRUD") != 0)
{
rg2d.velocity += new Vector2(Input.GetAxisRaw("Horizontal_LRUD") / 4, 0);
rg2d.velocity = new Vector2($$anonymous$$athf.Clamp(rg2d.velocity.x, -10, 10),rg2d.velocity.y);
}
}
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
Debug.Log("O$$anonymous$$");
footed = true;
}
}
The correct English for "footed" would be "grounded".
You could save a lot of code by simply having a velocity var that you modify then apply:
void FixedUpdate()
{
Vector2 velocity = rg2d.velocity;
float xInput = Input.GetAxis("Horizontal_LRUD");
float yInput = Input.GetAxis("Vertical_LRUD");
if (footed)
{
if (xInput != 0f)
velocity.x = xInput * 5;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
int xpower = 10, ypower = 10;
footed = false;
velocity.Set(xInput * xpower, ypower);
}
else if (yInput > 0f)
{
footed = false;
velocity.y = 6f;
}
}
else
{
if (xInput != 0)
{
velocity.x = $$anonymous$$athf.Clamp(
velocity.x + xInput / 4f,
-10f, 10f);
}
}
rg2d.velocity = velocity;
}
private void OnCollisionEnter2D(Collision2D col)
{
//TODO switch to a raycast based isGrounded, or the player will be able to jump when touching the ceiling or walls
if (col.gameObject.tag == "Ground")
{
Debug.Log("O$$anonymous$$ entering");
footed = true;
}
}
Please note that if you're using GetAxis ins$$anonymous$$d of GetAxisRaw, you switching direction rapidly might allow you to make jumps in any angle.
Also, why do you need two different types of jumps?
long jumps were problematic that's why I was just testing these three events. the real script is way too long. thank you btw for the help!
I couldn't understand the comment in the Collision part. the ground's tag is "ground". ceiling and walls are tagless.
Your answer
Follow this Question
Related Questions
NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers
rigidbody2D velocity zero? 1 Answer
Do rigidbodies (especially 2d) have limits on their velocities? 1 Answer
move 2d character affected by physics with velocity and/or add force 2 Answers
2D Geometry dash-like ship physics 0 Answers