- Home /
cant jump in 2d?
i have just watched the 2D Character Controllers video tutorial
and i haven't had problems working through it until it got to the jump part, my character doesn't jump when i click space, but the "vspeed" is working because if i walk off the edge of my ground the "vspeed" changes, showing my animation. i don't know if you know what im talking about so ill just show you the script and hopefully someone can tell me the problem.
**using UnityEngine; using System.Collections; public class charactercontrol : MonoBehaviour {
public float maxspeed = 2f;
bool facingright = true;
Animator anim;
bool grounded = false;
public Transform groundcheck;
float groundRadius = 0.2f;
public LayerMask whatisground;
public float jumpforce = 700f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatisground);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vspeed", rigidbody2D.velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2 (move * maxspeed, rigidbody2D.velocity.y);
if (move > 0 &&!facingright)
Flip();
else if (move < 0 && facingright)
Flip ();
}
void update()
{
//float jump = Input.GetAxis ("Vertical");
//anim.SetFloat ("jumpforce", Mathf.Abs (jump));
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2 (0, jumpforce));
}
}
void Flip()
{
facingright = !facingright;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}**
thanks.
Have you checked the value of "grounded"
if it is false every time then problem would in line code.
grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatisground);
you can try another way to check character is grounded or not as follow
grounded = Physics2D.Linecast(transform.position,groundcheck.position, 1 << Layer$$anonymous$$ask.NameToLayer("NameOfGroundLayer"));
once i changed it to that it no longer recognized the ground, with the one i have it recognizes the ground and when i fall off the edge, the parameter gets unticked and i go into my falling animation. i think the problem is when i press space nothing happens, no force is pushed up.
Please try replacing your jump code with this:
if (grounded && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
Debug.Log("Trying to jump");
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2 (0, jumpforce));
}
If when you run this code the message is printed to console, then check your gravity/mass values, otherwise check your input and ground check.
Answer by KellyThomas · Dec 23, 2013 at 01:26 PM
Sorry I was working from my phone earlier and could only see small areas of your code at a time.
Now I am at a PC I have noticed:
Your
Update
function should have a leading capital i.e. "`void update()`" should be "`void Update()`"Your update logic is spilt between FixedUpdate and Update? Is there a reason for this split?
Correcting the spelling of "`Update`" should fix the immediate problem.
Answer by clarke3018 · Dec 23, 2013 at 02:24 PM
oh dear it was that simple, I'm sorry for wasting your time hahaa all i had to do was put a capital letter on my void Update(), and its split so that the jump key doesnt get missed.
thankyou very much.
Answer by Lune · Mar 21, 2014 at 04:23 AM
i have found that if your sprite has an Animator component, you have to deactivate "apply root motion" because that causes conflict with the 2d physics
Answer by Dimension99 · Aug 30, 2016 at 10:05 AM
,I really don't know how to thank you very very very dear Kelly, I was stuck in this like more than a week and I really didn't know what to do to make it jump. you know, I'm kinda very new to unity and it didn't give me any error either. so I couldn't find the problem (Capitalizing U in "Update"). You did me a very very very great favor. Thank you so much. GOOOOOOOD LUCK @username
From: Aref Bikaran/ Kabul Polytechnic University Afghanistan
Your answer
Follow this Question
Related Questions
Flat 2d image on 3d character. 0 Answers
when 2d character hits wall it shakes/bounces 1 Answer
2D Character creator ,how can i save current clothes (changes) 1 Answer
Ui sidescroller 2d touch ? 0 Answers