- Home /
does not work check if dead
I have a check in my code: if(transform.position.y <= cameraTrans.position.y - 6f)
but this check doesnt work. help me please
Comment
Will need more information, can you post some more code and info on what is happening?
Of cource! there is a code. If the player leaves the camera at a height of - 6f isDead = true; game$$anonymous$$anagerScript.GameOver ();
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public Transform cameraTrans;
public Game$$anonymous$$anager game$$anonymous$$anagerScript;
public int score = 0;
float playerFirstYPos;
[SerializeField]
float moveSpd;
[SerializeField]
float jumpPower;
Rigidbody2D myRigidbody2d;
SpriteRenderer mySpriteRenderer;
bool isDead = false;
void Awake(){
myRigidbody2d = GetComponent<Rigidbody2D> ();
mySpriteRenderer = GetComponent<SpriteRenderer> ();
}
void Start(){
playerFirstYPos = transform.position.y;
}
void Update(){
FlipCharacter ();
CheckIfDead ();
SetScore ();
}
void FixedUpdate (){
Vector3 pos = myRigidbody2d.position;
pos.x = $$anonymous$$athf.Clamp (pos.x, -2f, 2f);
myRigidbody2d.position = pos;
//if delete go control touch
myRigidbody2d.AddForce (Vector2.right * Input.GetAxis ("Horizontal") * moveSpd);
//if delete go control touch
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
float middle = Screen.width / 2;
if (touch.position.x < middle)
{
myRigidbody2d.AddForce (Vector2.right * -1 * moveSpd);
mySpriteRenderer.flipX = true;
}
else if (touch.position.x > middle)
{
myRigidbody2d.AddForce (Vector2.right * 1 * moveSpd);
mySpriteRenderer.flipX = false;
}
}
else
{
}
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.CompareTag("Jumpable")){
myRigidbody2d.velocity = new Vector2 (myRigidbody2d.velocity.x, 0f);
myRigidbody2d.AddForce (Vector2.up * jumpPower);
}
}
void FlipCharacter(){
if (Input.GetAxisRaw ("Horizontal") < 0)
mySpriteRenderer.flipX = true;
else if (Input.GetAxisRaw ("Horizontal") > 0)
mySpriteRenderer.flipX = false;
}
void CheckIfDead(){
if(transform.position.y <= cameraTrans.position.y - 6f && !isDead) {
isDead = true;
game$$anonymous$$anagerScript.GameOver ();
}
}
void SetScore(){
score = $$anonymous$$athf.$$anonymous$$ax (score, $$anonymous$$athf.FloorToInt (transform.position.y - playerFirstYPos) * 10);
}
}
Thanks, so does it not enter the if condition in CheckIfDead() or it enters the condition but something else is not working? What is the exact problem?