• 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
1
Question by smnerat · Dec 09, 2012 at 03:24 AM · raycastcolliderparent

Trouble assigning transforms of two raycast hits to two different objects.

Hey,

In my scene I have six objects parented to an empty game object. I have two separate empty game objects. (Eventually there will be 20 or so empty game objects, with six child objects a piece.) What I am trying to do is assign the parent of a first object hit by a raycast to a variable and then assign the parent of a second object (with a different parent) to a second variable.

If a third object is selected I want to wrap around to the beginning, so the parent of a third selected object would be assigned to the first variable and the parent of the fourth object selected would be assigned to the second variable, etc.

The code I have below works to a certain degree. If I start the game and select one object, it works. When I try to click the second object, the first click assigns the parent of the first clicked object to the second variable. If I click it a second time, it is assigned correctly. Unfortunately this throws off my timing and counts with other scripts. I cant seem to clear the raycast or find another work around. I'm hoping someone can provide some insight.

 static var selectFirst : GameObject;
 static var selectFirstParent : GameObject;
 static var selectSecond : GameObject;
 static var selectSecondParent : GameObject;
 static var clickCount : int;
 var firstHit : RaycastHit;
 var secondHit : RaycastHit;
 
 
 function Update(){
 
     if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), firstHit, Mathf.Infinity) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), secondHit, Mathf.Infinity)){
    
     clickCount += 1;
     FirstClick();
     SecondClick();
         
     }
 }
 
 
 function FirstClick(){
 
         if (clickCount == 1){
     
         selectFirst = firstHit.collider.gameObject;
         selectFirstParent = selectFirst.transform.parent.gameObject;
         Debug.Log(clickCount);
         Debug.Log(selectFirstParent);
         }
         
         else{}
     }
         
         
 function SecondClick(){
 
         if (clickCount == 2){
     
         selectSecond = secondHit.collider.gameObject;
         selectSecondParent = selectSecond.transform.parent.gameObject;
         Debug.Log(clickCount);
         Debug.Log(selectFirstParent);
         clickCount = 0;
         }

         else{}

}

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 shane.rachel · Dec 09, 2012 at 03:54 AM 0
Share

what exactly is happening that you don't want to occur?

avatar image smnerat · Dec 09, 2012 at 04:49 AM 0
Share

After a first object has been clicked and assigned to the first variable "selectFirst". The second object has to be clicked twice to assign it to the variable "selectSecond". Since a double click is performed the clickCount is off as well. Im wondering if secondHit is storing the first object somehow?

1 Reply

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

Answer by aldonaletto · Dec 09, 2012 at 05:01 AM

There's something weird with this code: you cast two identical rays, thus the same object will be hit by the two rays! Furthermore, you're using collider.Raycast, which ignores all objects except the one to which the script is attached - is this script attached to each object?
If the script is attached to a single object (the camera or any other), you could use the following:

 static var selectFirst : GameObject;
 static var selectFirstParent : GameObject;
 static var selectSecond : GameObject;
 static var selectSecondParent : GameObject;
 static var clickCount : int = 0;
 
 function Update(){
   if (Input.GetMouseButtonDown(0)){ // if button pressed...
     var hit: RaycastHit; // cast a ray through the mouse position
     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)){
       switch (clickCount){ // check the state:
         case 2: // after 2 clicks, restart from the first object
         case 0: // nothing clicked yet: save first object ref
           selectFirst = hit.collider.gameObject;
           selectFirstParent = selectFirst.transform.parent.gameObject;
           clickCount = 1;
           break;
         case 1: // 1st object already clicked: save the 2nd
           selectSecond = hit.collider.gameObject;
           selectSecondParent = selectSecond.transform.parent.gameObject;
           clickCount = 2;
           break;
       } 
     }
   }
 }
Comment
Add comment · Show 2 · 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 smnerat · Dec 09, 2012 at 05:40 AM 0
Share

Thank you very much, this works perfectly. I'm fairly new to Unity and scripting in general. I've done a little with raycasting and knew I was doing something weird and wasn't quite able to straighten it out. I like the case function that you used, but I didn't know about it until now. Thanks again!

avatar image aldonaletto · Dec 09, 2012 at 11:32 AM 0
Share

A hint about the switch statement: remember to end each case block with a break statement, otherwise the execution continues through the following case. This can be useful sometimes, like in the case 2 block: I wanted it to continue in the case 0 block. At the end of case 0, however, I used a break to stop execution before case 1

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer

RaycastAll related question 1 Answer

Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer

How do I go about storing the last hit of a raycast and then using it for other stuff it on a character? 2 Answers

What is the best way to do stealth in Unity? 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges