- Home /
Answer by Julien-Lynge · Jan 31, 2013 at 10:33 PM
That sounds like a fun game! I'm sure others would like to do the same thing.
By the way, if you don't ask a question, you don't get an answer.
Here's a better answer for you. I hope it helps!
Your post (as it appears now) isn't something we can give you a quick answer for, and isn't really appropriate for UnityAnswers. The UnityAnswers philosophy is:
"[Unity Answers] is a place to ask specific questions that have specific answers. The forum is a better place to post discussions and non-technical questions."
It sounds like what you're looking for is Unity training, rather than a specific answer. I would suggest visiting the following training websites to find the one that best helps you move forward. In addition to the sites below, you can always search YouTube, which has a large number of user-created Unity tutorials.
3DBuzz (hover over the Unity dropdown) - http://www.3dbuzz.com/vbforum/sv_home.php
Lynda - https://www.lynda.com/
BurgZergArcade - http://www.burgzergarcade.com/
Unity3DStudent - http://www.unity3dstudent.com/
UnityGems - http://unitygems.com/
Answer by landon912 · Feb 01, 2013 at 02:47 AM
Break it down into smaller parts,
` `Learn how to get if the mouse is over the brick..pisss(OnMouseOver)
Then learn to get if you click on it...pisssss(Input.GetMouseButtonDown, inside of the above).
Then learn to get the mouse position...pissss(Input.mousePosition, I think!? I'm not on pc)
Then learn to move the brick...pissss(transform.Translate)
.
Next time I won't be so kind. Don't ask us to do your work...wait you didn't even ask a question. :p
I put in in a code format so it is positioned right, it's hard to do on mobile ;)
Thanks. and i did not ask you to do my work. I asked if you could help.
Answer by AlucardJay · Feb 01, 2013 at 05:11 AM
I guess you could call the comments here a lesson on how to ask a detailed question on Unity Answers. I have to interject as I feel OnMouse functions to be unreliable, what you should learn about is Raycast and Collider. Collider becomes a big thing in Unity, so is good to learn and understand. Raycasting returns so much information, it is harder to do things without it.
So now there are some things to start considering : colliders help identify objects and allow them to react physically; raycasts are great for detecting colliders and returning useful information about them.
Unity Scripting Reference links :
Colliders : http://docs.unity3d.com/Documentation/ScriptReference/Collider.html
Raycast : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Those are probably the most confusing links to read apart from Transform and GameObject, but use the Unity Scripting Reference (API) and things get explained well there.
But to help you start, I am going to do what is normally not allowed on this 'site, show you how to put all the different pieces together to do what you want.
Start with a mouse input : http://docs.unity3d.com/Documentation/ScriptReference/Input.html : leads to > > : http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html
function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
Debug.Log( "Pressed left click." );
}
}
Now we want to select a brick, and remember that brick. For this we store a reference to it. For now you are probably just moving the brick, so we shall store the type that is Transform.
var selectedBrick : Transform // a variable that is typecast to Transform to store the selected (chosen) brick
now for the fun part, to use raycast to find the brick that is clicked then store a reference to it ! You have the raycast link above. Scroll down to the very last example :
var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // from the camera in a direction based on mouse screen position
var hit : RaycastHit; // special variable type that returns information from colliders that were raycast
if ( Physics.Raycast( ray, hit, 100 ) ) // if ( a ray in the input direction, with the ray hit information to return, for a distane of 100 units from the origin )
{
Debug.DrawLine( ray.origin, hit.point ); // show a line from the start to where the ray first hit some collider
Debug.Log( "ray hit (name) : " + hit.collider.gameObject.name ); // the name of the gameObject of the collider that was hit
Debug.Log( "ray hit (tag) : " + hit.collider.gameObject.tag ); // you can find out lots about the object based on its components
}
Some interesting things here that come from one small raycast script. The links :
Ray : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html
RaycastHit : http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
Input.mousePosition : http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html
and Debug.DrawLine (excellent way to see what a raycast is doing while testing) : http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawLine.html
So put that raycast after a mouse input and it looks like this :
var selectedBrick : Transform // a variable that is typecast to Transform to store the selected (chosen) brick
function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
Debug.Log( "Pressed left click." );
var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // from the camera in a direction based on mouse screen position
var hit : RaycastHit; // special variable type that returns information from colliders that were raycast
if ( Physics.Raycast( ray, hit, 100 ) ) // if ( a ray in the input direction, with the ray hit information to return, for a distane of 100 units from the origin )
{
Debug.Log( "ray hit (name) : " + hit.collider.transform.name ); // the name of the gameObject of the collider that was hit
// Store a reference to that brick
selectedBrick = hit.collider.transform;
}
}
}
So really I havn't written your script, Unity has! When you get better at raycasts you can look at Layers and LayerMask , just type them into the search box at the top-left of any Unity Scripting Reference link on this answer. Now this is only half an answer but alot to think about and get started.
If you edit your question to include what you have tried so far, and provide code you are using, then you will have a better chance for better answers.
Definitely watch the unity3Dstudent series. Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/