- Home /
Question by
javert · Dec 12, 2013 at 08:37 PM ·
raycastmouseclick
Raycast not working!!! help me please
Hi, my script is not working properly, Works only with one game object, but in my scene have a 40 cube's clone frefab. When I click on any cube all of the cubes moves and Spawn. Can you help me to fix it? I need to click in one of the cubes and this cube will move not the others. I put the script on a cube, and create 4 minor cube to "setas" gameobject cube has tagged "blocos" directional cubes has tagged "cima","direita","baixo","esquerda"
using UnityEngine;
using System.Collections;
public class regraBlocos : MonoBehaviour {
private Vector3 selfSEtaPos1;
private Vector3 selfSEtaPos2;
private Vector3 selfSEtaPos3;
private Vector3 selfSEtaPos4;
static bool blocoAtivo = true;
static bool blocoPressionado = false;
public GameObject seta1;
public GameObject seta2;
public GameObject seta3;
public GameObject seta4;
// Use this for initialization
void Start () {
// selfPosition = new Vector3(((transform.position.x)- (transform.position.x)), ((transform.position.y)- (transform.position.y)), ((transform.position.z)- (transform.position.z)));
selfSEtaPos1 = new Vector3(((transform.position.x)+ (0)), ((transform.position.y)+ (0)), ((transform.position.z)+ (1)));
selfSEtaPos2 = new Vector3(((transform.position.x)+ (1)), ((transform.position.y)+ (0)), ((transform.position.z)+ (0)));
selfSEtaPos3 = new Vector3(((transform.position.x)- (0)), ((transform.position.y)+ (0)), ((transform.position.z)- (1)));
selfSEtaPos4 = new Vector3(((transform.position.x)- (1)), ((transform.position.y)+ (0)), ((transform.position.z)- (0)));
}
//void SpawnSetas1() {// regra para spawnar as setas somente uma vez
// int pushing = 0;
// if (pushing == 0){
// Instantiate(seta1,selfSEtaPos1, Quaternion.identity);
// Instantiate(seta2,selfSEtaPos2, Quaternion.identity);
// Instantiate(seta3,selfSEtaPos3, Quaternion.identity);
// Instantiate(seta4,selfSEtaPos4, Quaternion.identity);
// }
// pushing = 1;}
void Destroi_Setas(){// regra para destruir as setas quando nao e mais preciso
//destroi seta 1
//destroi seta 2
//destroi seta 3
//destroi seta 4
}
// Update is called once per frame
void Update(){
if (Input.GetMouseButtonDown(0) & blocoAtivo ==true)
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.transform.gameObject.tag == "BLOCO" && blocoAtivo ==true)
{blocoPressionado = true;
Instantiate(seta1,selfSEtaPos1, Quaternion.identity);
Instantiate(seta2,selfSEtaPos2, Quaternion.identity);
Instantiate(seta3,selfSEtaPos3, Quaternion.identity);
Instantiate(seta4,selfSEtaPos4, Quaternion.identity);;
Debug.Log(blocoPressionado);
}
//regra se o ray colidir com as tags o bloco se move
if (hitInfo.transform.gameObject.tag == "CIMA" & blocoAtivo ==true)
{transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1.0f);
blocoAtivo = false;
Destroi_Setas();
//por aqui e para os demais a regra de destruir setas
}
if (hitInfo.transform.gameObject.tag == "DIREITA" & blocoAtivo ==true)
{transform.position = new Vector3(transform.position.x + 1.0f, transform.position.y, transform.position.z);
blocoAtivo = false;
Destroi_Setas(); }
if (hitInfo.transform.gameObject.tag == "BAIXO" & blocoAtivo ==true)
{transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1.0f);
blocoAtivo = false;
Destroi_Setas(); }
if (hitInfo.transform.gameObject.tag == "ESQUERDA" & blocoAtivo ==true)
{transform.position = new Vector3(transform.position.x - 1.0f, transform.position.y, transform.position.z );
blocoAtivo = false;
Destroi_Setas(); }
}
}
}
}
Comment
Static variables share their value with all instances of the class, so if you modify it in one instance all of the rest will get it. You'll have to take the static keyword out of any variables that cause the cubes to move.