- Home /
I only want my character to jump when touching the ground
I am new to unity and C# and I got this far. and was wondering if someone could help me with the code for this last bit.
What I have is that the character will jump and jump as soon as he touches the ground due to the
"if collider.collision.tag == "Ground"" then the code down here to make him jump.
is there a way I can make it Only jump IF he is on the ground and I hit the spacebar/Mouse
Thanks in advance here is the code!
using UnityEngine;
using System.Collections;
public class Running : MonoBehaviour {
public float acceleration;
public Score score;
private float seconds = 1;
Animator animator;
public bool dead = false;
float deathCooldown;
public Vector3 jumpVelocity;
// used to get animations
void Start () {
animator = transform.GetComponentInChildren<Animator>();
if (animator == null) {
Debug.LogError ("Didn't find animator!");
}
}
// Update is called once per frame
void Update () {
if (dead) {
seconds -= 1 * Time.deltaTime;
if (seconds <= 0) {
Application.LoadLevel ("DeathScene");
}
} else {
if(Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
}
}
}
void FixedUpdate() {
if (dead)
return;
//Run Speed
transform.Translate (4.2f * Time.deltaTime, 0f, 0f);
}
//Collider to die
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Obstacle" || collision.collider.tag == "ObstacleBag" || collision.collider.tag == "ObstacleGarbage") {
animator.SetTrigger ("Death");
dead = true;
audio.Play();
}
if (collision.collider.tag == "Ground" || (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))) {
rigidbody2D.AddForce (Vector3.up * 185);
animator.SetTrigger ("DoJump");
}
}
}
Answer by dreamhex · Sep 28, 2014 at 03:12 PM
I think it's something like this.
public Transform groundCheck;
bool grounded = false;
public int jumpSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1<< LayerMask.NameToLayer("Ground"));
if ((Input.GetKeyDown("space"))&&(grounded))
{
rigidbody2D.AddForce(transform.up*jumpSpeed);
grounded = false; //so you can jump only once
}
}
You'll make a new GameObject and place it under your Player (no collider needed on GameObject, that white thing is Player :D ). Also sprite which represents ground should have layer named "Ground".
I added the code ect. I can't have the ground layer set to Ground, as I have it set to Background so it loops the ground.
is there a way to make it a tag?
and could you be more specific on what to do with the gameobject stuff? thanks in advance.
so far the code works just I'm not able to jump at all
I think that is because of the Layer$$anonymous$$ask. I can't have it set Ground as a layer because if i did that it would not loop my background. is there another way to do it?
here is the code
using UnityEngine;
using System.Collections;
public class Running : $$anonymous$$onoBehaviour {
public float acceleration;
public Score score;
private float seconds = 1;
Animator animator;
//
public Transform groundCheck;
bool grounded = false;
public int jumpSpeed;
//
public bool dead = false;
float deathCooldown;
public Vector3 jumpVelocity;
// used to get animations
void Start () {
animator = transform.GetComponentInChildren<Animator>();
if (animator == null) {
Debug.LogError ("Didn't find animator!");
}
}
// Update is called once per frame
void Update () {
if (dead) {
seconds -= 1 * Time.deltaTime;
if (seconds <= 0) {
Application.LoadLevel ("DeathScene");
}
} else {
grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << Layer$$anonymous$$ask.NameToLayer ("Ground"));
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (0) && (grounded)) {
rigidbody2D.AddForce (transform.up * 175);
animator.SetTrigger ("DoJump");
grounded = false;
}
}
}
void FixedUpdate() {
if (dead)
return;
//Run Speed
transform.Translate (4.2f * Time.deltaTime, 0f, 0f);
}
//Collider to die
void OnCollisionEnter2D(Collision2D collision) {
if (collision.collider.tag == "Obstacle" || collision.collider.tag == "ObstacleBag" || collision.collider.tag == "ObstacleGarbage") {
animator.SetTrigger ("Death");
dead = true;
audio.Play ();
}
}
}
The grounded check works, however force isn't being added, so it won't jump ? Am I doing something wrong ?
Answer by jmparavicini · Sep 28, 2014 at 01:47 PM
in java you could use if(is.Grounded) but im not sure if you can do the same in c#
Answer by Cherno · Sep 28, 2014 at 04:05 PM
Here is a tutorial that deals with basic character movement, including jumping.
https://www.packtpub.com/books/content/unity-3x-scripting-character-controller-versus-rigidbody
Answer by AfonsoLopez · Oct 06, 2016 at 07:52 AM
I have the same issue
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public Animator Anim;
public Rigidbody2D PlayerRigidbody;
public int Pulo;
public bool punch;
//verifacador de chao
public Transform GroundCheck;
public bool grounded;
public LayerMask whatisGround;
//soco tempo
public float socoTempo;
public float tempoTempo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// se o pulo for apertado e estiver pisando no chao, faca isso
if(Input.GetButtonDown("Jump") && grounded== true) {
PlayerRigidbody.AddForce(new Vector2(0, Pulo));
}
if(Input.GetButtonDown("Fire2")) {
punch = true;
tempoTempo = 0;
}
grounded = Physics2D.OverlapCircle(GroundCheck.position, 0.2f, whatisGround) ;
if(punch == true)
{
tempoTempo += Time.deltaTime;
if(tempoTempo >= socoTempo)
{
punch = false;
}
}
Anim.SetBool ("soco", punch);
}
}