- Home /
Objects Stuff
So I want to make a game where I can place things and build and stuff like the old Star Wars Galaxies game. So how can I make a object follow the mouse around and then on mouse click place the object at that point. Also it would be the nearest place on the ground or on top of another object already placed… I seem to be babbling now so if anyone has ideas please share with me.
Answer by HappyMoo · Jan 02, 2014 at 03:10 AM
No problem. You need some basic understanding how unity works.
It's best if you do a tutorial like this one: http://www.youtube.com/playlist?list=PLX2vGYjWbI0RibPF7vixmr4x8ONJX-mNd
Also check, there's a link to the accompanying tutorial website and the downloads of the needed asset package from the asset store in the description of the first video in that playlist.
Ok why is it that every time I ask something I get a response like this like I have never done anything in unity in my life. I was thinking about this idea and know I need to instantiate the object when I click and I know how to click also if you think that I don't know that. So respectfully was asking if anyone has a idea on how to go about this and would like to make some rough code to help me thanks.
It's how you ask looks like you haven't done anything yet. Saying you want to make a huge game and then asking the most basic stuff. It's not like you put your question in any context of what you already have etc.
But I wasn't trying to belittle you - I was just trying to help you in giving you the right information for what seemed to be your knowledge level.
"Follow the mouse around" is also not very clear. Do you want the item to move around the screen at mouse position? in the air or on the ground? In what distance from the camera?
Anyway, placing an object where you click can be achieved by raycasting from the camera and instantiating your prefab at the hitpoint.
Here's how you create the ray: http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html
Then use this ray to raycast it: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html (It's the last invocation pattern with the ray and the RaycastHit object)
Also make sure the floor and the objects if they should stack have colliders on them, or your ray will go right through them
Let me know if you need more help.
Ok got it work but now how can I make it visible at the location of the mouse and as I move the mouse it moves. This way I can preview what it would look like then click to keep one at that position… anything you could do to help would be great…
using UnityEngine;
using System.Collections;
public class Placement : $$anonymous$$onoBehaviour
{
public GameObject block;
void Start ()
{
}
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 13))
{
Instantiate(block, hit.point, Quaternion.identity);
}
}
}
}
Create a new layer "permanent" (I assume layer 20 for now) for your objects you will raycast against and put your floor in this layer, but your block prefab shouldn't be in it.
Now instantiate the object you want to place from the start:
private int permanentLayer = 20
private GameObject nextToPlace;
void Start()
{
nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
nextToPlace.SetActive(false);
}
then remove the check for the fire button, so you raycast all the time... and change your Raycast so you only check for the permanentLayer and also move your preview object to the hit position. Then if you click, let go of the placed object and get a new one to move around
if (Physics.Raycast(ray, out hit, 13, 1<<permanentLayer ))
{
nextToPlace.transform.position = hit.point;
nextToPlace.SetActive(true);
if (Input.GetButtonDown("Fire1"))
{
nextToPlace.mask = permanentLayer; // place it, so we can put things on top
// get a new one:
nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
nextToPlace.SetActive(false);
}
}
else
{
nextToPlace.SetActive(false);
}
You can also have a transparent material on it till you place it and take hit.normal for your rotation into account, if you want to place things on walls etc...
There may be syntax errors in the code - I don't try it, so treat it as pseudocode.
Ok got it working… also .mask does not exist it's .layer but anyways was thinking is there a way I can make some kind of grid that the objects snap to the closest of that way its easier to line objects up with each other? maybe even if control held snap to grid == false.
$$anonymous$$y idea is that each rectangular way I place will snap in line with each other. Anything that larger will just be based off of a certain number of rectangles wide and long. That way everything can snap together. And if necessary I could hold control to not snap to grid. Is this possible in unity and could this snap be shown before being placed like if you move your mouse out of a grid it snaps to the next? Could I draw the grid on the ground while I have my game in edit mode?
Your answer
Follow this Question
Related Questions
Making Building Placment rotatable 0 Answers
How to make drawable Paths 0 Answers
I need a grid of objects at runtime, but the gameObject is instantiated later 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers