2d up down platformer. Character has 2 Rays(one left one right) going down, when either Ray's hit distance hits between -1 and 1 it should call a function which will change character direction from down to up. Works 1, 2, 3, 4, sometimes 6, sometimes into the teens times then character goes through the obstacle got placing character over an obstacle and hitting play with no changes gave a different amount of time before falling through the obstacle. I have tried changing the Time.time in fixedUpdate, placed movement in last update, varying degrees of success, I will post the current version of code, but it has gone through several revisions any ideas accepted. thanks
[RequireComponent (typeof (BoxCollider2D))]
public class YowiController : MonoBehaviour {
BoxCollider2D yowiCollider;
Vector2 rayDir, leftRay, rightRay, rayOrigin;
public AudioSource jumpSound;
bool jump;
bool bounce;
public float upTime;
float looseTime;
float bounceCount = 0;
float gravity = 3f;
float bounceHeight = 5f;
float movementSpeed = 2f;
float bounceTime = .03f;
void Start () {
yowiCollider = GetComponent<BoxCollider2D> ();
rayDir = new Vector2 (0, -1);
jump = false;
bounce = false;
}
void Update () {
}
private void LateUpdate () {
movement ();
}
void movement () {
drawRays ();
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Translate (Vector2.right * Time.deltaTime * movementSpeed);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Translate (Vector2.left * Time.deltaTime * movementSpeed);
}
if (!bounce) {
transform.Translate (Vector2.down * Time.deltaTime * gravity);
gravity += .02f;
} else {
Bounce ();
}
}
void Bounce () {
transform.Translate (Vector2.up * Time.deltaTime * bounceHeight);
looseTime = bounceTime / 2;
if (upTime < looseTime) {
bounceHeight += .01f;
looseTime--;
} else {
bounceHeight -= .01f;
}
}
void drawRays () {
Bounds bounds = yowiCollider.bounds;
leftRay = new Vector2 (bounds.min.x, bounds.min.y);
rightRay = new Vector2 (bounds.max.x, bounds.min.y);
RaycastHit2D leftHit = Physics2D.Raycast (leftRay, rayDir * -1, .05f, 1 << 9);
RaycastHit2D rightHit = Physics2D.Raycast (rightRay, rayDir * -1, .05f, 1 << 9);
if (leftHit || rightHit) {
RayHit (leftHit, rightHit);
}
}
void RayHit (RaycastHit2D leftHit, RaycastHit2D rightHit) {
if ((leftHit.distance < 1.1f) && (leftHit.distance > -1.1f)) {
bounce = true;
StartCoroutine (Bouncing ());
} else if (rightHit.distance < 1.1f && rightHit.distance > -1.1f) {
bounce = true;
StartCoroutine (Bouncing ());
} else {
print ("No Joy");
}
}
IEnumerator Bouncing () {
yield return new WaitForSeconds (bounceTime);
bounce = false;
}
}