Hello,
I have a question I need answered asap.
So basically my script is used to tell if the player is grounded or not. I used a collider to tell if the collider is grounded. However, I have come into a problem. Since the cube can rotate, i need to have the collider on every edge. So heres my problem. When I am next to a wall, I can double jump, triple jump, quadripual jump, well keep jumping untill I get off the wall. I have made a temporary fix by telling my script to make all objects which have a certain tag (“notag”) on them, to make the cube not able to count them as a collider and to make grounded = false when they touch. This did somewhat solver my problem but lets say my cube is next to the wall, then the cube wont be able to jump because grounded is off when touching the wall. I want to make this script as dynamic as it can be so please help me.
#pragma strict
var speed : float = 20;
var jump : float = 5000;
var grounded = false;
function Start () {
}
function OnCollisionStay2D(other : Collision2D) {
if(other.gameObject.tag == "notag") {
grounded = false;
}
else {
grounded = true;
}
}
//BUG FIX : When touching notag, the player cant jump at all. Need it to see that there is a zone the player can jump at.
function OnCollisionEnter2D(other : Collision2D) {
if(other.gameObject.tag == "notag"){
grounded = false;
}
else {
grounded = true;
}
}
//BUG FIX : When player has collided with two objects and one collision leaves, grounded is false.
function OnCollisionExit2D(other : Collision2D) {
grounded = false;
}
function Update () {
if(Input.GetKey("d")) {
rigidbody2D.AddForce (Vector2(1, 0) * speed);
//transform.Translate(Vector3.right * Time.deltaTime * 10);
}
if(Input.GetKey("a")) {
rigidbody2D.AddForce (Vector2(-1, 0)* speed);
//transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
//transform.Translate(Vector3.left * Time.deltaTime * 10);
}
if(Input.GetKeyDown("space")){
if(!GravitySwitch.isOn){
if(grounded){
rigidbody2D.AddForce (Vector2(0, 1) * jump);
}
}
if(GravitySwitch.isOn){
if(grounded){
rigidbody2D.AddForce (Vector2(0, -1.2) * jump);
}
}
}
if(Input.GetKey("q")){
rigidbody2D.AddTorque (7.5);
}
if(Input.GetKey("e")){
rigidbody2D.AddTorque (-7.5);
}
}