Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class moveSphere : MonoBehaviour
- {
- public Rigidbody rb; // para trabalhar com aspetos físicos
- private int speed = 500; //NEWWW
- private Vector3 screenPoint, offset, initialPos, oldPos, ballSpeed, zeros;
- private List<Vector3> positions = new List<Vector3>(); //NEWWWW
- TrailRenderer currentTrail;
- Collider myCollider;
- public AudioSource soundEffect;
- public GameObject prefab, explosion, Cubo1, Cubo2, Cubo3;
- public Renderer objCubo1, objCubo2, objCubo3;
- // Start is called before the first frame update
- void Start()
- {
- currentTrail = GetComponent<TrailRenderer>();
- currentTrail.emitting = false;
- // currentTrail.widthMultiplier = 0.0f;
- rb = GetComponent<Rigidbody>();
- zeros.Set(0, 0, 0);
- initialPos = GetComponent<Rigidbody>().position;
- myCollider = GetComponent<Collider>();
- soundEffect = GetComponent<AudioSource>();
- }
- void OnCollisionEnter(Collision other) {
- // TODO: Inserir som de colisão
- if (other.gameObject.name == "esfera" && rb.name == "esfera")
- {
- soundEffect.Play();
- }
- if (other.gameObject.name == "trash_can" && rb.name == "esfera") {
- Debug.Log("trash");
- lockedScript ls = rb.gameObject.GetComponent<lockedScript>();
- ls.locked = true;
- rb.transform.GetComponent<Renderer>().material.color = new Color(1,1,1,0.43f);
- }
- if ((other.gameObject.name == "baliza1" || other.gameObject.name == "baliza2" || other.gameObject.name == "baliza3" || other.gameObject.name == "baliza3") && (rb.name == "esfera")) {
- lockedScript[] spheres = (lockedScript[]) GameObject.FindObjectsOfType(typeof(lockedScript)) as lockedScript[];
- GameObject obj = null;
- if (other.gameObject.name == "baliza1") {
- Cubo1 = GameObject.Find("bal1_1");
- objCubo1 = Cubo1.GetComponent<Renderer>();
- baliza sc = Cubo1.GetComponent<baliza>();
- var ass = sc.assigned;
- if (ass == true) {
- foreach (lockedScript sphere in spheres) {
- // Debug.Log(sphere.locked);
- if (sphere.locked == true) {
- sphere.locked = false;
- obj = sphere.gameObject;
- break;
- }
- }
- // caso nao haja mais bolas livres
- // FIXME: escolher uma sem ser a primaria etc
- if (obj == null) {
- objCubo1.material.color = Color.white;
- return;
- }
- Cubo2 = GameObject.Find("bal1_2");
- objCubo2 = Cubo2.GetComponent<Renderer>();
- objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
- baliza sc1 = Cubo2.GetComponent<baliza>();
- sc1.assigned = true;
- Color color1 = objCubo1.material.color;
- Color color2 = objCubo2.material.color;
- float newRed = (color1.r + color2.r)/2;
- float newGreen = (color1.g + color2.g)/2;
- float newBlue = (color1.b + color2.b)/2;
- Color newColor = new Color(newRed,newGreen,newBlue);
- other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
- // EXPLOSÃO AQUI
- foreach (ContactPoint contact in other.contacts)
- {
- explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
- explosion.GetComponent<Renderer>().material.color = newColor;
- }
- obj.GetComponent<Renderer>().material.color = newColor;
- // TODO: ESPERAR
- other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
- objCubo2.material.color = Color.white;
- objCubo1.material.color = Color.white;
- sc.assigned = false;
- sc1.assigned = false;
- } else if (ass == false) {
- objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc.assigned = true;
- }
- }
- if (other.gameObject.name == "baliza2") {
- Cubo1 = GameObject.Find("bal2_1");
- objCubo1 = Cubo1.GetComponent<Renderer>();
- baliza sc = Cubo1.GetComponent<baliza>();
- var ass = sc.assigned;
- if (ass == true) {
- foreach (lockedScript sphere in spheres) {
- // Debug.Log(sphere.locked);
- if (sphere.locked == true) {
- sphere.locked = false;
- obj = sphere.gameObject;
- break;
- }
- }
- // caso nao haja mais bolas livres
- // FIXME: escolher uma sem ser a primaria etc
- if (obj == null) {
- objCubo1.material.color = Color.white;
- return;
- }
- Cubo2 = GameObject.Find("bal2_2");
- objCubo2 = Cubo2.GetComponent<Renderer>();
- objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
- baliza sc1 = Cubo2.GetComponent<baliza>();
- sc1.assigned = true;
- Color color1 = objCubo1.material.color;
- Color color2 = objCubo2.material.color;
- float newRed = (color1.r + color2.r)/2;
- float newGreen = (color1.g + color2.g)/2;
- float newBlue = (color1.b + color2.b)/2;
- Color newColor = new Color(newRed,newGreen,newBlue);
- other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
- // EXPLOSÃO
- foreach (ContactPoint contact in other.contacts)
- {
- explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
- explosion.GetComponent<Renderer>().material.color = newColor;
- }
- obj.GetComponent<Renderer>().material.color = newColor;
- // TODO: ESPERAR
- other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
- objCubo2.material.color = Color.white;
- objCubo1.material.color = Color.white;
- sc.assigned = false;
- sc1.assigned = false;
- } else if (ass == false) {
- objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc.assigned = true;
- }
- }
- if (other.gameObject.name == "baliza3") {
- Cubo1 = GameObject.Find("bal3_1");
- objCubo1 = Cubo1.GetComponent<Renderer>();
- baliza sc = Cubo1.GetComponent<baliza>();
- var ass = sc.assigned;
- if (ass == true) {
- Cubo2 = GameObject.Find("bal3_2");
- objCubo2 = Cubo2.GetComponent<Renderer>();
- baliza sc2 = Cubo2.GetComponent<baliza>();
- var ass2 = sc2.assigned;
- if (ass2 == true) {
- foreach (lockedScript sphere in spheres) {
- // Debug.Log(sphere.locked);
- if (sphere.locked == true) {
- sphere.locked = false;
- obj = sphere.gameObject;
- break;
- }
- }
- // caso nao haja mais bolas livres
- if (obj == null) {
- objCubo1.material.color = Color.white;
- return;
- }
- Cubo3 = GameObject.Find("bal3_3");
- objCubo3 = Cubo3.GetComponent<Renderer>();
- objCubo3.material.color = rb.transform.GetComponent<Renderer>().material.color;
- baliza sc3 = Cubo3.GetComponent<baliza>();
- sc3.assigned = true;
- Color color1 = objCubo1.material.color;
- Color color2 = objCubo2.material.color;
- Color color3 = objCubo3.material.color;
- float newRed = (color1.r + color2.r + color3.r)/3;
- float newGreen = (color1.g + color2.g + color3.g)/3;
- float newBlue = (color1.b + color2.b + color3.b)/3;
- Color newColor = new Color(newRed,newGreen,newBlue);
- other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
- // EXPLOSÃO AQUI
- foreach (ContactPoint contact in other.contacts)
- {
- explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
- explosion.GetComponent<Renderer>().material.color = newColor;
- }
- obj.GetComponent<Renderer>().material.color = newColor;
- // TODO: ESPERAR
- other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
- objCubo2.material.color = Color.white;
- objCubo1.material.color = Color.white;
- objCubo3.material.color = Color.white;
- sc.assigned = false;
- sc2.assigned = false;
- sc3.assigned = false;
- }else {
- objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc2.assigned = true;
- }
- }else {
- objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc.assigned = true;
- }
- }
- if (other.gameObject.name == "baliza4") {
- Cubo1 = GameObject.Find("bal4_1");
- objCubo1 = Cubo1.GetComponent<Renderer>();
- baliza sc = Cubo1.GetComponent<baliza>();
- var ass = sc.assigned;
- if (ass == true) {
- Cubo2 = GameObject.Find("bal4_2");
- objCubo2 = Cubo2.GetComponent<Renderer>();
- baliza sc2 = Cubo2.GetComponent<baliza>();
- var ass2 = sc2.assigned;
- if (ass2 == true) {
- foreach (lockedScript sphere in spheres) {
- // Debug.Log(sphere.locked);
- if (sphere.locked == true) {
- sphere.locked = false;
- obj = sphere.gameObject;
- break;
- }
- }
- // caso nao haja mais bolas livres
- if (obj == null) {
- objCubo1.material.color = Color.white;
- return;
- }
- Cubo3 = GameObject.Find("bal4_3");
- objCubo3 = Cubo3.GetComponent<Renderer>();
- objCubo3.material.color = rb.transform.GetComponent<Renderer>().material.color;
- baliza sc3 = Cubo3.GetComponent<baliza>();
- sc3.assigned = true;
- Color color1 = objCubo1.material.color;
- Color color2 = objCubo2.material.color;
- Color color3 = objCubo3.material.color;
- float newRed = (color1.r + color2.r + color3.r)/3;
- float newGreen = (color1.g + color2.g + color3.g)/3;
- float newBlue = (color1.b + color2.b + color3.b)/3;
- Color newColor = new Color(newRed,newGreen,newBlue);
- other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
- // EXPLOSÃO AQUI
- foreach (ContactPoint contact in other.contacts)
- {
- explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
- explosion.GetComponent<Renderer>().material.color = newColor;
- }
- obj.GetComponent<Renderer>().material.color = newColor;
- // TODO: ESPERAR
- other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
- objCubo2.material.color = Color.white;
- objCubo1.material.color = Color.white;
- objCubo3.material.color = Color.white;
- sc.assigned = false;
- sc2.assigned = false;
- sc3.assigned = false;
- }else {
- objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc2.assigned = true;
- }
- }else {
- objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
- sc.assigned = true;
- }
- }
- }
- }
- void OnMouseDown()
- {
- lockedScript sc = GetComponent<lockedScript>();
- if (sc.locked != true) {
- oldPos = initialPos;
- screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
- offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
- Color forTrail = rb.transform.GetComponent<Renderer>().material.color;
- TrailRenderer myTrail = GetComponent<TrailRenderer>();
- myTrail.material.color = forTrail;
- }
- }
- void OnMouseDrag()
- {
- lockedScript sc = GetComponent<lockedScript>();
- if (sc.locked != true) {
- var curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
- Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - offset;
- transform.position = curPosition;
- currentTrail.emitting = false;
- if ((curPosition.z > -550 && curPosition.z < 550 && (curPosition.x > 270 || curPosition.x < -270)) || (curPosition.x < 270 && curPosition.x > -270 && (curPosition.z < -550 || curPosition.z > 550)) ) {
- myCollider.enabled = false;
- }
- // if (curPosition.x > 270 || curPosition.x < -270 || curPosition.z > 550 && curPosition.z < -550)
- // {
- // myCollider.enabled = false;
- // }
- else
- {
- myCollider.enabled = true;
- }
- }
- }
- void OnMouseUp()
- {
- lockedScript sc = GetComponent<lockedScript>();
- if (sc.locked != true) {
- var curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
- Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - offset;
- //se a bola for largada dentro dos limites da mesa
- if (curPosition.x < 270 && curPosition.x > -270 && curPosition.z < 550 && curPosition.z > -550)
- {
- ballSpeed = (GetComponent<Rigidbody>().position - positions[0]); //NEWWWWW
- currentTrail.emitting = true;
- }
- else
- {
- currentTrail.emitting = false;
- rb.position = initialPos;
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- myCollider.enabled = true;
- }
- }
- }
- private void FixedUpdate()
- {
- oldPos = GetComponent<Rigidbody>().position;
- if(positions.Count < 10) //NEWWWWW
- {
- positions.Add(oldPos);
- } else
- {
- positions.RemoveAt(0);
- positions.Add(oldPos);
- } //ATÉ AQUI
- if(ballSpeed != zeros)
- {
- rb.AddForce(ballSpeed * speed);
- ballSpeed.Set(0, 0, 0);
- }
- }
- void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.CompareTag("Pick Up"))
- {
- other.gameObject.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement