Hello, I am making 2D game, but stuck with simple problem: I have cube game object, with material on it with changing textures over time. It works fine, but now I need to use OnMouseDown() function to destoy this cube, here is my code:
var duckSpeed: int;
private var directionX: int = 1;
private var directionY: int = 1;
private var tempTimer: float = 0;
private var animTimer: float = 0;
private var isDead: boolean = false;
var duckAnim: Texture2D[];
function Update () {
if(tempTimer < 8)
duckFlying();
else
duckFlyAway();
tempTimer += Time.deltaTime;
if(isDead){
Destroy(gameObject);
}
}
function OnMouseDown()
{
isDead = true;
}
function duckFlyAway()
{
if(isDead)
return;
//some code
}
function duckFlying()
{
if(isDead)
return;
amtToMove = duckSpeed * Time.deltaTime;
if(animTimer > 0.5){
renderer.material.SetTexture("_MainTex", duckAnim[0]);
if(animTimer >= 1)
animTimer = 0;
}
if(animTimer < 0.5){
renderer.material.SetTexture("_MainTex", duckAnim[1]);
}
if(transform.position.y >= 14)
directionY = -1;
if(transform.position.y <= -6)
directionY = 1;
if(transform.position.x >= 20)
{
directionX = -1;
renderer.material.SetTextureScale ("_MainTex", Vector2( 1, -1));
}
if(transform.position.x <= -20.5)
{
directionX = 1;
renderer.material.SetTextureScale ("_MainTex", Vector2( -1, -1));
}
transform.Translate(Vector3.up * amtToMove * directionY);
transform.Translate(Vector3.right * amtToMove * directionX);
animTimer += Time.deltaTime;
}
I cant delete my cube immidiattely, only after 3-6 seconds, dont know why , please help anyone