- Home /
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{}
}
what exactly is happening that you don't want to occur?
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?
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;
}
}
}
}
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!
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