Sunday, 9 October 2016

Follen Object Script

bool exp;
float scale1;
float scale2;
public float speed;
public float size;
public bool fallen;
public float timeToDown;
private Rigidbody2D rb2d;

// Use this for initialization
void Start () {
exp = false;
scale1 = transform.localScale.x;
scale2 = transform.localScale.y;


if (gameObject.GetComponent<Rigidbody2D> () != null) {
rb2d = gameObject.GetComponent<Rigidbody2D> ();
rb2d.isKinematic = true;
}
}


// Update is called once per frame
void LateUpdate () {
if (!fallen) {
if (!exp) {
if (transform.localScale.x < (scale1 * size)) {
transform.localScale = Vector2.Lerp (transform.localScale, new Vector2 (scale1 * size, scale2 * size), Time.smoothDeltaTime * speed);

if (transform.localScale.x >= (scale1 * (size * 0.9))) {
exp = true;
//Debug.Log (exp);
}
}
}

if (exp) {
if (transform.localScale.x > (scale1)) {
transform.localScale = Vector2.Lerp (transform.localScale, new Vector2 (scale1, scale2), Time.smoothDeltaTime * speed);

if (transform.localScale.x <= (scale1 * 1.1)) {
//Debug.Log (exp);
exp = false;
}
}
}
}
}

void OnCollisionStay2D(Collision2D coll)
{
if (fallen) {
if (coll.gameObject.tag == "Player") {
StartCoroutine(Fallen());

}
}
}

void OnCollisionEnter2D(Collision2D coll)
{
if (fallen) {
if (coll.gameObject.tag == "Ground" || coll.gameObject.tag == "Water" || coll.gameObject.tag == "Enemy" || coll.gameObject.tag == "moving" ) {
Destroy(gameObject,0.1f);

}
}
}

IEnumerator Fallen() {
// print(Time.time);
yield return new WaitForSeconds(timeToDown);
if (rb2d != null) {
rb2d.isKinematic = false;
}

}

No comments:

Post a Comment