I’m very confused as to why this won’t work, all I’m trying to do is make the box-collider drop down a little when my slide function is called, everything else is working fine but if anybody can explain to me why this doesn’t work it’d be highly appreciated.
using UnityEngine;
using System.Collections;
public class JumpScript : MonoBehaviour {
Animator anim;
public AudioClip JumpSound;
public AudioClip SlidingSound;
public float jumpSpeed = 1000f;
float jumpRate = 1.0f;
bool canJump = true;
public float slideTime = 1.0f;
public float timeHeld;
void Start()
{
this.gameObject.AddComponent<AudioSource>();
this.GetComponent<AudioSource>().clip = JumpSound;
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey(KeyCode.Space)) timeHeld += Time.deltaTime;
if (Input.GetKeyUp(KeyCode.Space) && timeHeld <= slideTime) TryJump();
if (Input.GetKey(KeyCode.Space) && timeHeld > slideTime) Slide();
timeHeld = 0f;
}
void TryJump()
{
if (!canJump) return;
Jump();
StartCoroutine(JumpCooldown());
}
IEnumerator JumpCooldown()
{
canJump = false;
yield return new WaitForSeconds(jumpRate);
canJump = true;
yield break;
}
void Jump()
{
audio.PlayOneShot(JumpSound);
rigidbody2D.AddForce(new Vector2(0, jumpSpeed));
anim.Play("Jumping");
}
void Slide()
{
BoxCollider2D.transform.Translate(new Vector2(0, -1));
anim.Play("Sliding");
}
}