- Home /
How to make your 2D character Wall Jump?
Hey guys, I'm new to Unity and scripting. I've been trying to find info on how to make your 2D sprite wall jump between two walls and keep the character stuck on the wall until the jump button is pressed again, but whatever I find always gives me a problem. When people post a question regarding wall jumps they already have some code written down but I haven't got any, would anyone please help me out? I've provided a photo of how I want my character to wall jump.
Answer by hassonhamo3 · Jul 27, 2018 at 06:39 PM
there's an easy way to make it happen and i do believe it's the easiest , in player script :
Rigidbody2D rb;
void Start (){ rb = GetComponent(); }
void OnCollisionEnter2D(Collision2D other){ if (other.gameObject.tag == "Wall") >>> in unity change the wall tag to Wall , otherwise it won't work. { rb.gravityScale = 0f; >>>>> in here we just change the gravity which allows the player to stuck on the wall .
}
}
void OnCollisionExit2D(Collision2D col){ if (col.gameObject.tag == "Wall") { rb.gravityScale = 3f; >>> bring back the default gravity , you could change the number to whatever you want . } }
Hey I followed what you said and it gave me the following errors:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : $$anonymous$$onoBehaviour
{ Rigidbody2D rb; }
// Use this for initialization
void Start()
{
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Wall")
{
rb.gravityScale = 0f;
}
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.tag == "Wall")
{
rb.gravityScale = 3f;
}
}
}
By the way, the code you gave me tells me how to stick my character onto the wall, but I also need help with how to make it wall jump as well, so I want it to jump between walls kind of like this game: https://youtu.be/4Gi5rRqzZ3$$anonymous$$
You have a } after your rigidbody2s rb; -- therefore, ending the class.
Your answer
Follow this Question
Related Questions
Question about Scripting a slingshot mechanic 0 Answers
Is there a way to lock velocity? 3 Answers
Enemy bounce from screen edges 0 Answers
Transform.translate bullets problem 0 Answers
Object following a path and colliding with other objects with physics. 1 Answer