Of course what is happening in the video is the player is just adding colour to a block but the general principle of the game is placing actual blocks down and together etc. You select an object to place down, it appears at the mouse cursor (or center of the screen if you are in first person view) and a click places the object.
I had a script that would (after pressing the “1” key) create a cube at the mouse cursor position and then follow it around, this however was undesirable because the object would be anywhere the mouse was (such as up in the air) and not “attached” to the plane. I wanted to know how to keep the object grounded so it if you move the mouse “up” it just goes further away (but not too far so some sort of depth limitation), “down” and it comes in closer etc etc.
You can have a collider to your plane and then use a raycast from the mouse pointer until you will hit the ground.
Done that you can place you object at the collision point occurred.
If no collisions occur, you will simply not place the cube.
The easy way is to change the y-value of the objects transform to 0 (or whatever your ground plane is).
Somewhat more complicated but more accurate would be to use a raycast from the camera to the mouse pointer position, checking where the ray connects with the ground.
using UnityEngine;
public class CubePoser : MonoBehaviour {
public GameObject cubePrefab;
void Update () {
if (Input.GetMouseButtonDown(0)) {
RaycastHit vHit = new RaycastHit();
Ray vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(vRay, out vHit, 1000)) {
GameObject.Instantiate(cubePrefab, vHit.point, Quaternion.FromToRotation(Vector3.up, vHit.normal));
}
}
}
}
Secondly i was looking at the code and im trying to work out how to maximize and minimize the depth at which the object can be placed. Is this something to do with [if (Physics.Raycast(vRay, out vHit, 1000))] <-------- the 1000. Can this be a value that places it no closer than about 2 units but no further than 15 say for instance. How would i change this, can you put a range in there?
Thirdly i wrote this script for “viewing” the object before it is placed as the plan is to press a key (say “1” for example) or a GUI button and the object would appear at the mouse location but would be grounded and rotatable until the mousebutton was clicked then it would be swapped out for a prefab of the real object. This would be so i could use a prefab that consists of a simple object to show where placement will be and then swap for a primary object on mouseclick. This is how far i got but as you will see it does not keep the object “grounded” and does not have a distance range in which it can be seen.
using UnityEngine;
using System.Collections;
public class PlaceHolderScript : MonoBehaviour {
public float depth = 10;
public float speed = 10;
// Use this for initialization
void Start ()
{
Screen.showCursor = false;
}
// Update is called once per frame
void Update ()
{
float rotateSpeed = speed * Time.deltaTime;
Vector2 mousePos = Input.mousePosition;
Vector3 wantedPos = Camera.main.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, depth));
transform.position = wantedPos;
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up, rotateSpeed);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.up, - rotateSpeed);
}
}
}
Like i say this is as far as i got but it does not work as desired.
I’m no expert but i’m guessing that the cube is spawned there because of it’s pivot point being in the middle. In Maya i just freeze/reset transformations.
If the prefab is just a unity model, perhaps try making an empty gameobject at 0,0,0 and parenting everything in there.
Or simply, spawn it as you are now, and just shift the object up by half it’s height once you spawned it.