- Home /
Creating a GameObject between two
As shown in the image below, I have 5 game object placed on the screen(There can be many more)And this objects are instantiated at runtime when certain events occurs,since this elements are instantiated at runtime they could be placed randomly and each sphere can be dragged as well.Now what I want is when user touch any two objects one after the other a line should connect them (rather cylinder/capsule in unity) for example if user touches sphere1 and sphere2 one after the other then Sphere1 and Sphere2 should be connected.And once any these objects are connected,they cant be connected again.I just tried with two spheres and that too with fixed distance between them and no dragging as well.So can any one suggest how to go about it.
The part where i am stuck is with the connection of two spheres.It can be found which two elements are touched using colliders. Don't know how to go about it.I mean the distance between two spheres will be the length of the cylinder which will connect them.How to deter$$anonymous$$e that.Am completely new to unity may be thats why i am not able to do it.
Answer by sanks007 · Jul 29, 2013 at 07:01 AM
Thanks @robertbu i got started with something.atleast i am now able to create a cylinder between two objects.Will look for algorithm for determining how many GameObjects are there on the screen.Here is the code which i tried.May be you can point out a bit more with it.
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit))
{
sphereIndex = testscript.sphereCollider.IndexOf ( hit.collider );
if ( sphereIndex!= -1 )
{
matched=true;
touchCount++;
if(touchCount==1)
{
currentelementIndex=sphereIndex;
previouselementIndex=currentelementIndex;
firstElement=testscript.elementArray[previouselementIndex];
firstElementValencyInt=testscript.elementValency[sphereIndex];
currentposition=firstElement.transform.position;
previousposition=currentposition;
}
else if(touchCount==2)
{
bondTypeCount=1;
currentelementIndex=sphereIndex;
touchCount=0;
secondElement=testscript.elementArray[currentelementIndex];
currentposition=secondElement.transform.position;
secondElementValencyInt=testscript.elementValency[sphereIndex];
if(firstElement.name!=secondElement.name)
{
if (pos2 != pos1)
{
cylinderbond= Instantiate ( singleBond,previousposition,Quaternion.identity );
v3 = previousposition-currentposition;
cylinderbond.transform.position = currentposition + (v3) / 2.0;
cylinderbond.transform.localScale.x = v3.magnitude/4.0;
cylinderbond.transform.position.z=singleBond.transform.position.z-3;
cylinderbond.transform.rotation = Quaternion.FromToRotation(Vector3.right, v3);
cylinderbond.transform.localScale.y=1;
}
}
}
}
}
}
The spheres mentioned in this question can take both touch and drag events.The problem that I am encountering is in some phones,while creating connection ins$$anonymous$$d of taking Input.Get$$anonymous$$ouseButtonDown(0)
it takes touch.phase
events, so ins$$anonymous$$d of creating connection i end up dragging the Spheres. How can i avoid this ? And another thing while creating bond between two spheres the surface of cylinder gets pixelated. What could be the reason for this
Answer by gregzo · Jul 27, 2013 at 08:50 AM
To calculate the distance between the spheres, you can use Vector3.distance.
But what you want is a line, not a cylinder I suspect. You can use the linerenderer, or invest a few bucks in the excellent line drawing library Vectrosity.
Cant pay that much as i am an indie developer and have just started with something.Thanks for the distance calculation part. Linerenderer will see if it fits in the design of the app.
Answer by robertbu · Jul 27, 2013 at 09:02 AM
There are a number of different technical problems in your question. You are going to have to break it apart and figure out how to solve each.
First you are going to have to figure out how to detect the object you click on. The best way for your game is going to be Raycasting().. There are a bizillion questions and answers in Unity Answers on Raycasting(), plus see the reference page to Physics.Raycast().
You are going to have to distinguish between a click and a drag, since you say a user can drag the spheres. See the Input class and the OnMouse*() functions.
You are likely going to have to communicate to scripts on the spheres to mark and clear selections, plus also to mark the sphere as 'taken' when a line is placed. See Accessing Other Game Objects.
Then you are going to need to position a line or a the cylinder. If you just want to use a line, Vectrosity from the Asset store is a great package. If you want to go the cylinder route, I've posted several answers on how to place, rotate, and scale object to fit between to other positions. You can find one post here.
Note Unity Answers is not designed for this kind of discussion with multiple technical questions. Unity Forums is a better fit. At UA we try to restrict questions to a single technical issue.
@robertbu .Thanks for such a great reply.Detailing about each and every problem that can be faced.Now going point by point
I am able to do the first point as u said deter$$anonymous$$ing which gameobject is clicked using Physics.Raycast().
There are issue with drag. I am designing this for mobile so using Input classes.If i am able to post it on forums then i will describe it completely what problem am facing with drag.That itself is a big problem which i am planning to post out here.
Third part of the answer you said communicating between various Game Object is also done.By creating a Variable of the GameObject and accessing variables defined in the script as well.
The fourth part that's where am stuck up.I cant pay that much so will look into the link you suggest and try and find out what can be achieved. Will get back to you soon
This is the link of the forum question please try and help me
Any one of the points above, by itself, is appropriate for Unity Answers. Including your source code attempt, will make it even more appropriate. From the link to the question I provided you in point #4, the important lines of code for your problem are these:
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = v3.magnitude/2.0;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
These lines scale, rotate, and position a built-in cylinder between the two Vector3 positions pos2 and pos1. The two things 'special' about the built-in cylinder is that it is 2 units high, and the two sides that need to start and end at the positions is the top and the bottom. If you are using other geometry, then you will have to change the lines as appropriate to the scale and sides used.
Thank you so much for your answer! You made life much easier for me.