- Home /
Diagonal Wall Jump
Hey everyone! I want my player jumps automatically and diagonally when I click the spacebar I'm going crazy to figure out how to do it and as the last option I decided to ask for help here, i hope you can help me T_T my player has rigidbody so i tried with addforce... i tried with transform.translate too but it's like i need to press the spacebar without interruption to get the diagonal movement, instead i need that the player does this jump with only a tap...
Answer by blueLED · Nov 13, 2014 at 09:35 PM
First, zero out your player's y velocity when he hits the wall (makes things more accurate):
rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
Then apply the wall jump force:
rigidbody.AddForce(Vector3.up * wallJumpForce, ForceMode.VelocityChange);
hi, thanks for the reply! i forgot to say that it is a 2D game some of the code works greatly: when i press the spacebar it moves automatically and when it hit the wall it stops moving... but there are some problems.. there's no VelocityChange in Force$$anonymous$$ode2D and it doesn't go diagonally but only upwards, so i changed Vector3.up in Vector2.right but it doesn't go diagonally ... with what can i replace the Force$$anonymous$$ode.VelocityChange and how can i get it to move diagonally? another question.. now the rigidbody velocity of the player when it's colliding with the wall is 0, so how can i get the player to move again(while it is colliding with the wall) in the other direction and diagonally when i press the spacebar again? i'm sorry for all these questions and my terrible english D: this is what i wrote:
public int wallJumpForce = 10;
void Update (){
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) {
rigidbody2D.AddForce(Vector2.right * wallJumpForce, Force$$anonymous$$ode2D.Impulse); // i tried for no reason Impulse;
}
}
void OnCollisionEnter2D (Collision2D other) {
if (other.gameObject.tag == "wall"){
rigidbody2D.velocity = new Vector3(rigidbody2D.velocity.x, 0, 0);
}
}
rigidbody2D.velocity = new Vector2(-rigidbody2D.velocity.x, rigidbody2D.velocity.y);
That code was based on a 3D platformer I made. So I'm not sure about 2D. First I made wall-sliding work. Then I did the wall-jumping. I used raycasts to check what side the wall was on, the left or right. Based on that raycast, I would also apply an extra force to propel the player away from the wall. The combined forces made it go diagonal.
Start by adding a force randomly so you can see that it works. Then get the raycasting to work so you know which side to apply the force.
thanks for the help again anyway i've solved most of the problems.. the only problem i still have is get it to go diagonally how can i say to unity to go upwards and right(or left) to get the diagonal movement with only one press? o.O
i solved it!! anyway thanks blueLed for having tried to help me
Your answer
Follow this Question
Related Questions
Jumping away from a wall 1 Answer
Making an immovable object 0 Answers
Smart camera? 1 Answer
Moving objects with attached colliders not colliding? 1 Answer
Simple Jump script 1 Answer