- Home /
I found a work around for the problem that allowed me to get it working.
Spawning off size blocks
I have the following scrip allowing me to spawn blocks in my scene when I do it on a standard size cube they stack just fine but if I go to any size other size then that they tend to not stack correctly. Most of the time they stack inside the one you are trying to spawn it on top or side of.
using UnityEngine;
using System.Collections;
public class CubeBuilder : MonoBehaviour
{
float range = Mathf.Infinity;
public GameObject currentBlock;
public Transform curBlock;
RaycastHit hit;
public GameObject piece;
public GameObject piece1;
private Transform child;
Vector3 hitPoint;
private Rect checkRect = new Rect(0,0,160,160);
public Ray ray;
private GameObject[] blocks;
void Start ()
{
blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
}
void Build ()
{
ray= Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray,out hit, 1000))
{hitPoint = hit.transform.position + hit.normal;
Instantiate(currentBlock, hitPoint, curBlock.rotation);
}
}
void Erase ()
{
ray= Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(blocks.Length> 50)
{
if (Physics.Raycast (ray, out hit, 1000))
{
piece1 = hit.transform.gameObject;
piece = piece1.GetComponent<ParticleHolder>().ruble;
hitPoint = hit.transform.position + hit.normal;
Instantiate(piece, hitPoint,curBlock.rotation);
Destroy(hit.transform.gameObject);
}
}
}
void Update ()
{
blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
if (!checkRect.Contains(Input.mousePosition))
{
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
}
}
Answer by robertbu · Jul 24, 2013 at 06:38 PM
Your problem is on line 31:
hitPoint = hit.transform.position + hit.normal;
The hit.normal has unit length (length of 1), so your hitPoint is exactly one unit away from the center of the object hit. To fix your code, you need to scale hit.normal by the size of your blocks. Assuming you've uniformly scaled on all three axes to to 'scale' size:
hitPoint = hit.transform.position + hit.normal * scale;
If the blocks are not uniformly scaled, then you are going to have to figure out what direction the hit.normal is facing and use the scale for the appropriate axis.
Ok my blocks are not uniform because some of them are a lot thinner in one direction then the other.
Then you are going to have to figure out which axis of the block was hit. Do you have blocks of different sizes, or are they all sized the same (i.e. I know the they are not uniformly scaled, but is the length width and height the same for all blocks)? Do the blocks align with the world axes or are they rotated? Vector3.Dot() is what I'd use to detect the axis, but how will depend on the answers to these questions.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Setting Spawn Rotation correct 1 Answer
Spawning A Grid Of Cubes 2 Answers
Object Spawning Too Low 0 Answers
Distribute terrain in zones 3 Answers