• 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 /
  • Help Room /
avatar image
0
Question by gemmaF · Apr 16, 2018 at 01:52 PM · raycastmouseclick

Detect multiple objects position on mouse click?

Hi everyone! I would like to ask you a question about detectin an obj position since I’m not so skilled at coding in c#, I’m still learning. I am working on a project for my game design master, which is a rubik’s cube turned into an art gallery (like in the photo), with no colors, just the traditional movements of the rows and columns. It is made with 27 cubes as children of an empty obj set as parent. The cube is currently moving thanks to the advanced Rubik's cube notation (so keyboard inputs) but I’m trying to add also swipe movements up, down, left and right on mouse click (getmousebittondown(0) and then getmousebuttonup(0)). Before starting to work on the swipe script, my professor suggested to detect the position of the cubes on the scene with Raycasting for mouse left click. I tried to do that (here there is the piece of code I’m working on), and in the console I have the positions on mouse click on the 9 cubes of just a face of the whole parent cube. My question is: how should I detect the other mouse positions on the other faces of my cube since my main camera is facing just one of the faces of the cube? I tried to add more cameras assigning a different display but I’m not sure this is the right way to do that since also on the same cubes I’m detecting different positions switching between cameras. Thank you for your help! alt text

CODE: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ClickPositionManager : MonoBehaviour {

 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 clickPosition = -Vector3.one;

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if (Physics.Raycast(ray, out hit))
         {
             clickPosition = hit.point;
         }

         Debug.Log(clickPosition);
     }

     if(Input.GetMouseButtonUp(0))
     {
        Vector3 clickPosition = -Vector3.one;

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if (Physics.Raycast(ray, out hit))
         {
             clickPosition = hit.point;
         }

         Debug.Log(hit.point);
     }


     /*if (Input.mousePosition.y > mouseDownPos.y)
     {
         Debug.Log("You dragged up!");
     }
     else if (Input.mousePosition.y < mouseDownPos.y)
     {
         Debug.Log("You dragged down!");
     }*/
 }

}

schermata-2018-04-16-alle-154235.jpg (277.6 kB)
Comment
Add comment · Show 2
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 tormentoarmagedoom · Apr 16, 2018 at 04:21 PM 0
Share

Good day.

You said:

"$$anonymous$$y question is: how should I detect the other mouse positions on the other faces of my cube since my main camera is facing just one of the faces of the cube? "

I supose you mean :

$$anonymous$$y question is: how should I detect the other cube positions on the other faces of my cube since my main camera is facing just one of the faces of the cube?

Is this right?


Why you can not calculate where they are? You know the movements, you can know where are all the cubes if know where started and what movements you did.

With raycast, you can only interact with what "you can see", nad multiple cameras, as you said maybe is not a good idea.

You can create a script with a GameObject array for all cubes, and assing them at the beggining. And ask for their positions with

Debug.Log (CubesArray[numberOfCube].transform.position);

If this don't solve the problem, give more information of what you expect to get!


If helped, accept the answer and close the question! Bye!

avatar image gemmaF tormentoarmagedoom · Apr 17, 2018 at 09:07 PM 0
Share

Hi! Sorry for replying this late. I tried what you've suggested and I'm receiving this message in the console: "IndexOutOfRangeException: Array index is out of range. CubesPositions.Update () (at Assets/Script/CubesPositions.cs:21)". I've created a new script like you said but maybe I wrote something wrong, here it is. using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CubesPositions : $$anonymous$$onoBehaviour {

 public GameObject[] cubes;

   

 // Use this for initialization
 void Start()
 {
     //Debug.Log(cubes[27].transform.position);
 }

 // Update is called once per frame
 void Update()
 {

     Debug.Log(cubes[27].transform.position);
 }

}

I attached it to my cube parent and then I assigned all the cubes to the array.

$$anonymous$$y cube is currently moving with keyboard inputs according to this but I would like to add the swipe movements of the mouse to move up, down, left and right columns and rows like you can do here.

I'm not quiet sure about how to do that efficiently.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

How to get gameobject of script with raycast? 0 Answers

Deselecting all the other Units when selecting a Unit 0 Answers

Raycast + Collider + Mouse 0 Answers

User Layer that Ignores Raycast? 2 Answers

How do you detect a mouse button click on a Game Object? C# 2 Answers


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