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");
}
}
}
