- Home /
ground detection fails
Hello I am Brazilian so my English are not perfect well I am noob with C# So when I discovered how to make a ground check using OnCollisionEnter I make a party so how my game Is a puzzle game I have the brilliant idea to make at Universal tag to almost all objects in the scene so I had no problem jumping but this system has a few bugs the first one well I used OnCollisionEnter to verify if you are in the ground and how I said before I created down Universal tag to almost all objectives in the scene and if you touch object in left or right you could jump no matter where you are you could jump i reutilized this bug like a wall jump But it is Disturbed by the second bug
the second bug how I said before I used OnCollisionEnter to verify if you're in the ground I discovered that if you get out the ground without jumping you could jump in the air once i Ignored this by a Long Time but Correct this bug was turning into something almost essential because i was adding New mechanics that needed to be on the ground to work correctly like the break So I thinked “ if have OnCollisionEnter So must have OnCollisionExit” And have so I used OnCollisionExit To correct the bug it's supposed to work fine but it doesn't every two jumps you gave, the on ground verification fails It would not be bad if it not disturbs my wall jumps here is my code I'm accepting any help
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class player : MonoBehaviour { //ints public int lifes = 3; private int pickUpType1Num; private int pickUpType1Count = 0; private int pickUpType2Num; private int pickUpType2Count = 0; private int pickUpType3Num; private int pickUpType3Count = 0;
//floats
//don't care about they
private float speed = 5.0f;
private float runMutiply = 4.0f;
private float maxRotSpeed = 25.0f;
private float drag = 0.5f;
private float rateOfFire = 5.0f;
private float nextShot;
//vectors and transforms
//don't care about they yet
private Vector3 rotated;
private Transform cam;
public Transform USSP;
//coponents
//don't care about they,not yet
private Rigidbody rb;
private TrailRenderer trail;
public ParticleSystem runEffect;
//bools
private bool onGround;// only care about this one XD
//don't care about they
public bool runLock = false;
private bool passed = false;
//texts
public Text info1;
public Text info2;
public Text lifesText;
//etc
public GameObject cubeShot;
private void Start ()
{
//get coponent...
rb = GetComponent<Rigidbody>();
trail = GetComponent<TrailRenderer> ();
//get object? what??
pickUpType1Num = GameObject.FindGameObjectsWithTag ("pickUpType1").Length;
pickUpType2Num = GameObject.FindGameObjectsWithTag ("pickUpType2").Length;
pickUpType3Num = GameObject.FindGameObjectsWithTag ("pickUpType3").Length;
//etc
rb.maxAngularVelocity = maxRotSpeed;
rb.drag = drag;
cam = Camera.main.transform;
onGround = true;
setTexts ();
}
private void Update ()
{
if(Input.GetKey(KeyCode.X) && Time.time > nextShot){
Instantiate (cubeShot, USSP.position, USSP.rotation);
nextShot = Time.time + rateOfFire;
}
setTexts ();
jumpAndBrake ();//calling the jump function
walkAndRun ();
if (passed && GameObject.FindWithTag ("obstacle").GetComponent<ParticleSystem> ().particleCount == 0) {
GameObject.FindWithTag ("obstacle").SetActive (false);
}
}
//the OnCollisionExit function and the start of the on ground check
void OnCollisionExit(Collision any){
if (any.collider.tag == "MultiversalCollidingInertObject") {
onGround = false;
}
}
//verify if the ball is on the ground
void OnCollisionEnter(Collision any){
if (any.collider.tag == "MultiversalCollidingInertObject") {
onGround = true;
}
}
//you can ignore everything after it XD
void OnTriggerEnter(Collider other) {
if(other.CompareTag("pickUpType1")){
other.gameObject.SetActive (false);
pickUpType1Count++;
}
if(other.CompareTag("pickUpType2")){
other.gameObject.SetActive (false);
pickUpType2Count++;
}
if(other.CompareTag("pickUpType3")){
other.gameObject.SetActive (false);
pickUpType3Count++;
}
if (pickUpType1Count == pickUpType1Num) {
passed = true;
GameObject.FindWithTag ("obstacle").GetComponent<ParticleSystem> ().Stop();
}
}
void setTexts(){
lifesText.text = "lifes " + lifes.ToString ();
info1.text = "purple squares:" + pickUpType1Count.ToString ();
}
void walkAndRun(){
//define direction vector
Vector3 dir = Vector3.zero;
//set the direction based on the control comand
dir.x = Input.GetAxis ("Horizontal");
dir.z = Input.GetAxis ("Vertical");
//normalize... for some reason...
if (dir.magnitude > 1) {
dir.Normalize();
}
//align direction to the camera position
Vector3 rotDir = cam.TransformDirection (dir);
rotDir = new Vector3 (rotDir.x, 0.0f, rotDir.z);
rotDir = rotDir * dir.magnitude;
//walk,or... run!
if (Input.GetKey (KeyCode.LeftControl) && runLock) {
rb.AddForce ((rotDir * speed) * runMutiply);
trail.enabled = true;
runEffect.Play ();
} else {
rb.AddForce ((rotDir * speed));
trail.enabled = false;
trail.Clear ();
runEffect.Pause ();
runEffect.Clear ();
}
}
void jumpAndBrake(){
//brake
if (Input.GetKey (KeyCode.LeftShift) && onGround) {
rb.velocity = new Vector3 (rb.velocity.x / 1.25f, rb.velocity.y, rb.velocity.z / 1.25f);
}
//jump!now!
if (Input.GetAxis ("Jump") > 0 && onGround) {
rb.velocity = new Vector3(rb.velocity.x,7,rb.velocity.z);
//onGround = false;
}
}
}
thanks and bye