Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ceruleanmacaw · Dec 06, 2019 at 02:29 AM · clickselection

Need help selecting a game Object to move it

Hello, I'm new to Unity and I'm making my first game ever in this engine.

I currently have a 3 dimensional grid of positions for cubes, and they must move 1 position at a time in order to form a path with some faces, similar to a rubiks cube but only with translation, no rotation.

I have implemented a RayCast to select with a click which cube to move (cube movement through keypad (awsd))

but I have made a mistake! I need that once one cube is selected, the player has enough time to move the cube, since right now I have to click and press the keys very fast for it to kinda-work. Is there a way to adapt this code for the game to wait until the player moves the cube and "hold" the selection?

This is the click script I have attached to the camera to select a cube to move

Script: Click

 [SerializeField]
     private LayerMask clickableLayer;
 
     void Start()
     {
     }
 
     void Update()
     {
         if(Input.GetMouseButtonDown(0))
         {
             RaycastHit rayHit;
              
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity, clickableLayer))
             {
                 Debug.DrawRay(transform.position, transform.forward, Color.green); print("Hit"); 
                 rayHit.collider.GetComponent<block>().moveIt();
             }
         }

And I have another script attached to each cube, with the method that the click calls.

Script: Block

  void Update()
     {
         if (currentlySelected)
         {
             moveIt();
         }
     }
 
     public void moveIt()
     {
 
             myRender.material = matSelect;
 
             while (set != true)
             {
                 if (GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free == true)
                 {
                     transform.position = GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().transform.position;
                     GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = false;
                     set = true;
                 }
                 else
                 {
                     RandomPos();
                 }
             }
 
             if (Input.GetKeyDown(KeyCode.W)) // x dimension
             {
                 if (GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free == true)
                 {
                     transform.position = GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().transform.position;
                     GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = true;
                     GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = false;
                     x++;
                 }
 
            //and same thing with the other dimensions, this is getting kind of long
 
             }
 
         myRender.material = matStand;
         currentlySelected = false;
         
     }
 
     private void RandomPos()
     {
         x = Random.Range(0, 3);
         y = Random.Range(0, 3);
         z = Random.Range(0, 3);
     }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Anis1808 · Dec 06, 2019 at 04:12 AM

All you have to do is change this

 rayHit.collider.GetComponent<block>().moveIt();

To this

 rayHit.collider.GetComponent<block>().currentlySelected = true;

And make the variable currentlySelected public so the Click script can access it.

Comment
Add comment · Show 1 · Share
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 ceruleanmacaw · Dec 06, 2019 at 04:18 PM 0
Share

Thank you very much! That worked perfectly!

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

117 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 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 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 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 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 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 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

click first, click second, destroy if matched 1 Answer

selected object in array lost in translation 0 Answers

Some Copies of Prefab Detect Clicks But Others Don't 1 Answer

Is there a way to click on a render texture to select soemthing within the view? 4 Answers

GameObject becomes 'unselectable' 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