How to fix the blocks placement?
I have a script that places blocks. But when I look down, it places one on the top of me. How to fix it?
using UnityEngine;
using System.Collections;
public class Build : MonoBehaviour
{
public GameObject prefabToCreate;
public Transform sillouetteTransform;
Vector3[] positions = new Vector3[256];
int currentArrayPos = 0;
Ray ray;
RaycastHit raycastHitInfo;
Vector3 placePos;
bool canBuild = true;
bool canShowSlt = true;
void FixedUpdate()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out raycastHitInfo, 10))
{
placePos = new Vector3(Mathf.Floor(raycastHitInfo.point.x), Mathf.Floor(raycastHitInfo.point.y), Mathf.Floor(raycastHitInfo.point.z));
for (int i = 0; i <= 255; i++)
{
if (positions[i] == placePos)
{
canShowSlt = false;
break;
}
}
if (canShowSlt)
{
sillouetteTransform.gameObject.SetActive(true);
sillouetteTransform.position = placePos;
} else
{
canShowSlt = true;
sillouetteTransform.gameObject.SetActive(false);
}
if (Input.GetMouseButtonDown(1))
{
for (int i = 0; i <= 255; i++)
{
if (positions[i] == placePos)
{
canBuild = false;
break;
}
}
if (currentArrayPos > 255)
{
canBuild = false;
}
if (canBuild)
{
Instantiate(prefabToCreate, placePos, new Quaternion());
positions[currentArrayPos] = placePos;
currentArrayPos += 1;
}
else
{
canBuild = true;
}
}
}
}
}
Also, there are other placement bugs.
Please, somebody help! I don't want to ask it on StackOverflow because if my question will be not well-recieved, I'll be banned from asking the questions.
Will someone respond? Often, blocks can't be placed!
For god's sake, please wait at least a couple of hours before bumping your thread like that. You posted the question 38 $$anonymous$$utes ago, not a very long time for people to notice your thread, read it, try to understand it, find the error, and write a solution.
The first thing you should do is inserting Debug.Log lines to get an idea which parts of your code cause problems.
Also: "there are other placement bugs" is not very descriptive. f you need help, provide as much information as possible. Added points for describing what your code does in detail and posting schematics.
Answer by ollobin · Jun 17, 2016 at 03:43 PM
Please give more detail. However what I think is happening is that at the start you say if the raycast hits the floor the block is created, or so I think. I might be wrong. But when you say that, and you look down, obviously the raycast hits the floor right under you, and the block can't be made inside of you, so it goes on top of you. I hope this helps, but please be more specific as to what you want.
The problem:
void FixedUpdate() { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out raycastHitInfo, 10)) { placePos = new Vector3(Mathf.Floor(raycastHitInfo.point.x), Mathf.Floor(raycastHitInfo.point.y), Mathf.Floor(raycastHitInfo.point.z)); for (int i = 0; i <= 255; i++)<
Your answer
