- Home /
WALL collision detection problem... help!!!!
hi . i have a 2d game and i have a player with a script attached to him as u can see bellow.on the other hand i have some game objects as walls .
im using capsul colider with is trigger off and rigid body with use gravity on him but no is kinematic,on my player . and its better to mention that ive set all kinds of collision detection from district to continiuous dynamic on my player but nothing changed . right now its set to district . and also ive freezed my player on x and z position and also rotation .
and on my wall game object we have no script ... and im just using a box colider with no is trigger .
now the problem is that when i jump on the wall , player can walk on top of that and every things ok . but if i colide with wall from left or right or down side , i mean for example if i go straight through a wall left or right side , player passes through the wall !!!!
i just want to stop passing through walls and be able to recognize them az an real wall and colide with them ! so i really need your help .
this is my player entire script , i just put the whole script available for u for better understanding but dont pay attention to some of those comments , cuz their not related to this part ! :
var playerSpeed : int;
var playerRunSpeed : int;
var jumpHeight : float;
var extraJumpHeight : float;
var fireBallPrefab : Rigidbody;
var playing : boolean;
static var score = 0;
static var coins = 0;
static var lives = 3;
function Start ()
{
spawn();
playing = true;
}
function Update ()
{
if (playing)
{
//1- player movement
amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime; // amount to move the player
transform.Translate(Vector3.right * amtToMove, Space.World);
//------------------------------------------------------------------------
//2- player rotation...!
if (Input.GetKey("left") || Input.GetKey("a"))
{
transform.rotation = Quaternion.Euler(0,180,0);
}
if (Input.GetKey("right") || Input.GetKey("d"))
{
transform.rotation = Quaternion.Euler(0,0,0);
}
//------------------------------------------------------------------------------------------------------------------------
//3- player jump
// player can countinusly jump when you hold the up button. if its transform position in y axis is (on the ground)
//here should add extra jump code later
// extra jump ?X?
if ( (Input.GetKey("up") || Input.GetKey("w") )&& Mathf.Abs(rigidbody.velocity.y) <0.01 )
{
audio.Play();
rigidbody.velocity.y = 4.5;
amtToJump = jumpHeight + Time.smoothDeltaTime ;
transform.Translate(Vector3.up * amtToJump );
}
//--------------------------------------------------------------------------------------------------------------
//4- instantiate fireBall
if (Input.GetKeyDown("space"))
{
var tempfireBall : Rigidbody;
tempfireBall = Instantiate(fireBallPrefab,Vector3(transform.position.x , transform.position.y, transform.position.z),transform.rotation);
}
}
}
//--------------------------------------------------------------------------------------------------------------
//5- onGUI function/scoring part...
function OnGUI()
{
GUI.Label(Rect(10,10,120,20), "Score: " +playerScript.score);
GUI.Label(Rect(10,30,120,20), "coins: " +playerScript.coins);
GUI.Label(Rect(10,50,120,20), "lives: " +playerScript.lives);
}
//--------------------------------------------------------------------------------------------------------------
//6- player collisions
function OnCollisionEnter(other : Collision)
{
// ?X? ===> here we want to check direction of the collision ...?!
//if(other.gameObject.tag == "leftBody" || other.gameObject.tag == "rightBody")
if(other.gameObject.tag == "enemy" )
{
playing = false;
playerScript.lives -= 1;
amtToJump = jumpHeight + Time.smoothDeltaTime ;
transform.Translate(Vector3.up * amtToJump );
transform.position.z = -1;
yield WaitForSeconds(3);
if (playerScript.lives == 2)
{
Application.LoadLevel(1);
}
if (playerScript.lives == 1)
{
Application.LoadLevel(2);
}
if (playerScript.lives == 0)
{
Application.LoadLevel(3);
playerScript.score = 0;
playerScript.coins = 0;
playerScript.lives = 3;
}
}
if(other.gameObject.tag == "deathZone") {
playing = false;
playerScript.lives -= 1;
if (playerScript.lives == 2)
{
Application.LoadLevel(1);
}
if (playerScript.lives == 1)
{
Application.LoadLevel(2);
}
if (playerScript.lives == 0)
{
Application.LoadLevel(3);
playerScript.score = 0;
playerScript.coins = 0;
playerScript.lives = 3;
}
}
if(other.gameObject.tag == "flag") {
var playerTransform = GameObject.Find("flag").transform;
playerTransform.parent = transform;
}
if(other.gameObject.tag == "door")
{
playing = false;
gameObject.renderer.enabled = false;
yield WaitForSeconds(1.5);
Application.LoadLevel(4);
playerScript.score = 0;
playerScript.coins = 0;
playerScript.lives = 3;
}
if(other.gameObject.tag == "teleportGate1")
{
if (Input.GetKey("down") || Input.GetKey("s"))
{
print("xxx2");
transform.position.z = 0.8;
}
}
if(other.gameObject.tag == "teleportGate2")
{
Application.LoadLevel(5);
}
if(other.gameObject.tag == "doorToLevel1")
{
playing = false;
gameObject.renderer.enabled = false;
yield WaitForSeconds(1.5);
Application.LoadLevel(0);
}
}
//--------------------------------------------------------------------------------------------------------------
//7- player spawn function...
function spawn()
{
transform.position = GameObject.FindGameObjectWithTag("spawnPoint").transform.position;
}