- Home /
How to swap 2 gameobjects after 2 click!
my test:
var hit : RaycastHit;
var noObj : Transform = null;
var switchObj : Transform = null;
var tempObj : Transform;
function Update()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if(!noObj)//no object picked yet
{
if (Physics.Raycast (ray, hit, 100))
{
noObj = hit.transform;//save picked objects transform
tempObj.transform.position = noObj.transform.position;
}
}
if(noObj !=null)//if noObject now has a transform
{
if (Physics.Raycast (ray, hit, 100))
{
switchObj = hit.transform;
DoTheSwitch();
}
}
}
}
function DoTheSwitch()
{
noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
switchObj.transform.position = tempObj.transform.position;
}
It looks good to me, could you add more info like, the scene setup, or add some debug lines to see if Physics.Raycast is true, and what are your results with this test?
That's what I wanted to do, but how can i detect these two sprites ? :D
Perameters are a good feature, you can do an on click feature that adds one to a float or int, and have the animation work when it = 1 and the second when it = 2...
Now, with 3D objects test i have this problem .. one of object swapping but the second Remained in place
Did you change the type of tempObj
to Vector3
(from Transform
) and used it like in the example in my answer?
tempObj = noObj.transform.position
Answer by ScaniX · Jul 30, 2016 at 09:04 PM
I think three things are missing.
1) an else between your ifs, so you are not using the clicked object as first AND second object.
2) clearing noObj after the switch for the next click
3) tmpObj should be a Vector3
function Update()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0) && Physics.Raycast (ray, hit, 100))
{
if(!noObj)//no object picked yet
{
noObj = hit.transform;//save picked objects transform
tempObj = noObj.transform.position;
} else if (noObj !=null) { //if noObject now has a transform
switchObj = hit.transform;
DoTheSwitch();
}
}
}
function DoTheSwitch()
{
noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
switchObj.transform.position = tempObj;
noObj = null;
}
(I also shortened it a bit, but that doesn't change the logic)
Answer by cdnDave · Jul 29, 2016 at 09:16 PM
What happens when you run your test code? It looks like you are missing a line in your DoTheSwitch function to assign the noObj position to tempObj before moving it. I think it needs to be
function DoTheSwitch()
{
tempObj.transform.position = noObj.transform.position; //Cache the position of noObj before moving it
noObj.transform.position = switchObj.transform.position;//moves the first clicked object to the second clicke objects position
switchObj.transform.position = tempObj.transform.position; //move the second object to the cached position of the first object
}
Not working :/ my game is 2D game and when i click on one of gameobjects it display this msg " pick one of these objects "
Answer by ToasterKyle · Jul 31, 2016 at 11:32 AM
It is because the tempObj Transform object is never created, you need to either change tempObj to Vector3 or Instantiate the tempObj before setting the position.
As already stated in my answer. As you cannot really instantiate a transform, you should just use a Vector3.