Hello all, I’m making a 2d puzzle game. Currently my movement works great with 1 exception. If i am at a corner and i move away from 1 of the two objects I’m touching. Then my oncollisionexit function is triggered and allows me to move through the other object if i never stopped touching it. So I think I need someway for it to check if I am still collided with my values before resetting them to null, but I’m not sure how to do that if you even can.
#pragma strict
var speed : int = 2;
private var collided_with : GameObject[];
var left : boolean = true;
var right : boolean = true;
var top : boolean = true;
var bot : boolean = true;
var hasCollided : boolean = false;
var impact : boolean = false;
var twoFalse : int = 0;
function Awake () {
collided_with = new GameObject[2];
}
function Update () {
if(Input.GetAxis("Horizontal") > 0){ // this code makes the ship go right
if(hasCollided == true impact == false){
if(collided_with[0].transform.position.x >= transform.position.x){
right = false;
impact = true;
twoFalse++;
}
}
if(collided_with[1] != null twoFalse <= 2){
if(collided_with[1].transform.position.x >= transform.position.x){
right = false;
twoFalse++;
}
}
if(right == true){
transform.Translate(Vector3(1 * speed * Time.deltaTime, 0, 0));
}
}
if(Input.GetAxis("Horizontal") < 0){ // this code makes the ship go left
if(hasCollided == true impact == false){
if(collided_with[0].transform.position.x <= transform.position.x){
left = false;
impact = true;
twoFalse++;
}
}
if(collided_with[1] != null twoFalse <= 2){
if(collided_with[1].transform.position.x <= transform.position.x){
left = false;
twoFalse++;
}
}
if(left == true){
transform.Translate(Vector3(-1 * speed * Time.deltaTime, 0, 0));
}
}
if(Input.GetAxis("Vertical") < 0){ // this code makes the ship go down
if(hasCollided == true impact == false){
if(collided_with[0].transform.position.y <= transform.position.y){
bot = false;
impact = true;
twoFalse++;
}
}
if(collided_with[1] != null twoFalse <= 2){
if(collided_with[1].transform.position.y <= transform.position.y){
bot = false;
twoFalse++;
}
}
if(bot == true){
transform.Translate(Vector3(0, -1 * speed * Time.deltaTime,0));
}
}
if(Input.GetAxis("Vertical") > 0){ // this code makes the ship go up
if(hasCollided == true impact == false){
if(collided_with[0].transform.position.y >= transform.position.y){
top = false;
impact = true;
twoFalse++;
}
}
if(collided_with[1] != null twoFalse <= 2){
if(collided_with[1].transform.position.y >= transform.position.y){
top = false;
twoFalse++;
}
}
if(top == true){
transform.Translate(Vector3(0, 1 * speed * Time.deltaTime,0));
}
}
} // end of void update
function OnCollisionEnter(col : Collision){
if(collided_with[0] == null){
collided_with[0] = col.gameObject;
hasCollided = true;
}
else{
collided_with[1] = col.gameObject;
}
}
function OnCollisionExit(col : Collision){ // sets collided with to null after it hits something
collided_with[0] = null; [B] // This is the part im having a problem with, i need this to not get set to null if its still touching an object[/B]
collided_with[1] = null; [B] // but if i stop touching one of the objects they both get cleared[/B]
hasCollided = false;
if(left == false){
left = true;
twoFalse--;
}
if(right == false){
right = true;
twoFalse--;
}
if(top == false){
top = true;
twoFalse--;
}
if(bot == false){
bot = true;
twoFalse--;
}
impact = false;
}
Thanks for the help, and sorry if i have been posting a lot of questions today new to Unity.