using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GasolineScript : MonoBehaviour
{
public GameObject Gasoline;
public GameObject Coin;
int n = 0, moveSpeed = 6, ok;
float aux1,aux2;
bool isMoving = false;
Vector2 dir, pos;
GameObject gasoline;
Rigidbody2D gasolineRB;
BoxCollider2D gasolineCol;
void Start()
{
SpawnCoins();
}
void Update()
{
if (isMoving)
{
gasolineRB.MovePosition(gasolineRB.position + dir * moveSpeed * Time.fixedDeltaTime);
}
if (Input.GetMouseButtonDown(0))
{
gasoline = Instantiate(Gasoline, new Vector2(-2.7f , 0.1f), Quaternion.Euler(0f, 0f, 180f)) as GameObject;
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
isMoving = true;
gasoline.AddComponent<Rigidbody2D>();
gasolineRB = gasoline.GetComponent<Rigidbody2D>();
gasolineCol=gasoline.GetComponent<BoxCollider2D>();
dir = (pos - new Vector2(-2.7f, 0.1f) ).normalized;
}
//rotation
transform.eulerAngles = new Vector3(0, 0, n);
n++;
}
void SpawnCoins()
{
aux1 = Random.Range(-3.9f, -1.55f);
aux2 = Random.Range(-0.9f, 0.9f);
ok = 1;
for(;ok==1;)
{
ok = 0;
if((aux1>=-3f && aux1<=-2.4f) && (aux2<=0.4f && aux2 >= -0.2))
{
ok = 1;
}
if (ok == 1)
{
aux1 = Random.Range(-3.9f, -1.55f);
aux2 = Random.Range(-0.9f, 0.9f);
}
}
GameObject coin= Instantiate(Coin, new Vector2(aux1,aux2), Quaternion.identity) as GameObject;
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Coin")
{
Destroy(collider.gameObject);
SpawnCoins();
}
if (collider.gameObject.tag == "Border")
{
Destroy(gameObject);
}
}
}