8
2012
Object placement & pickup with mouse
Testing.. related to this topic: http://forum.unity3d.com/threads/118581-Placing-object-on-mouse-down
Controls:
- Left mouse button, place object
- While placing object, hold mousebutton down to keep moving the object
- With left mouse button, you can pick previously added objects (hold button down to move them)
Webplayer: (flash)
http://unitycoder.com/upload/demos/mMousePlacementPick1/
Download example scene: (Unity3.5)
mMousePlacement1.unitypackage
Source: (messy js)
Attach this to camera, add some prefab.
Note. the prefab needs to be on layer #8
// mouseplacement & pickup - v1.0 - 08.01.2012 - mgear - http://unitycoder.com/blog/
// instantiate objects with mousebuttondown, while button is held down, move the object at mouse cursor
// also you can pick previously added object and move it around (button held down)
#pragma strict
public var ObjectToPlace:Transform; // prefab to instantiate
private var clone:Transform; // hold instantiated object on this variable
// mainloop
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
var layerMask = 1 << 8; // check help page "layerMask", here we only cast ray on layer 8 (prefab layer)
// first we check if we hit previously added prefab
if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
{
clone = hit.transform; // grab the object that we hit, so we can move it around
}else{ // we didnt hit prefab, check if are will hit something else
layerMask = ~layerMask; // invert layermask, so we dont hit layer 8 (which is for prefabs)
if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
clone = Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
}
}
if(Input.GetButtonUp("Fire1")) // button released
{
clone=null; // clear clone variable
}
if(Input.GetButton("Fire1")) // mousebutton is held down
{
if (clone!=null) // if there is some object in clone variable
{
var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit2 : RaycastHit;
var layerMask2 = 1 << 8;
layerMask2 = ~layerMask2; // we are casting rays on other layers, except 8
if (Physics.Raycast (ray2, hit2, Mathf.Infinity, layerMask2)) // we hit something
{
clone.position = hit2.point; // move our object there
}
}
}
}
Related Posts
7 Comments + Add Comment
Leave a comment
Recent posts
- Run webgl application locally using python
- [LudumDare53] Delivery
- Dungeon Crawler Jam 2023: Trials Of the Mage
- [GreaseMonkey] Display “Unity Version Added” info in API Docs
- Fake 3D Parallax Effect using Shader & (Pre-generated) Depth Map
- Using OpenAI API with WebRequest from Unity
- [LudumDare52] Theme: Harvest
- Painting with Compute Shader
- Draw Pseudo Hilbert Curve
- Unity Stable Diffusion plugin
- Testing Connected Component Labeling
- (news) Nano Tech for Unity
Recent Comments
- on [GreaseMonkey] Display “Unity Version Added” info in API Docs
- on [Asset Store] Point Cloud Viewer & Tools
- on [Asset Store] Point Cloud Viewer & Tools
- on LineRenderer with Outline Shader
- on UI Text TypeWriter Effect [Script]
- on UI Text TypeWriter Effect [Script]
- on 2D Raycasting Visibility – Voxel Travelsal
- on Publish .ipa file from Windows PC to iOS without Mac
An article by












why “Error while importing package: Couldn’z decompress package”? But my Unity is 3.5 Pro.
hmm..seems that the file is corrupted, ill update it later today.
*link fixed
Thank you very much!
Hello there. Great example for object picking !
The problem with this script is that the prefab ( gameobject ) change his place when it clicked by the mouse. ( the half of the prefab become above the plane )
i want to change the prefab like instead of sphere i want to use a cube. how can i do this?
in the inspector you can assign another object (prefab) to “ObjectToPlace”
check prefab tutorial,
http://unity3d.com/learn/tutorials/modules/beginner/editor/prefabs-concept-usage