Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Josh707 · Dec 12, 2013 at 09:43 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Click + Raycast + Colliders 2 Answers

2D - Overlaying Colliders - MouseDown not detected 0 Answers

Push object in direction of camera 0 Answers

How to destroy an object using Raycast or OnMouseButtonDown ? 3 Answers

Moving objects with mouse button. raycast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges