Search Unity

How do i prevent placing blocks inside player

Discussion in 'Scripting' started by vanLiere, Jul 29, 2017.

  1. vanLiere

    vanLiere

    Joined:
    Jul 11, 2017
    Posts:
    7
    Hello, I have a very basic start of a voxel game set up, it got a few chunks and you can place and delete cubes. I can place objects inside myself and get stuck when i aim down to the floor.

    How can i prevent one from doing this? The player's average height is about 2 units and a block is one unit.
    Any pointers would be helpfull

    I was thinking to compare the position of the mouse with the plater's position and if the positions equals then disable the placing feature with a + 1 height on the player (bottom and top part). Also for entities like animals, monsters, npc's (in the future when i get there) (not entities like items though)

    I have a white transparent cube as a indicator where your block would be placed/removed i could copythis to a red one whenever the placement is invalid i think.

    My blockplacer script (if you need more info, tell so)

    Thanks in advance :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BlockController : MonoBehaviour
    5. {
    6.      // Set up here
    7.      public LayerMask lm;
    8.      public GameObject bhPrefab;
    9.      GameObject bh;
    10.      // Execute here
    11.      void Awake()
    12.      {
    13.          bh = GameObject.Instantiate(bhPrefab, Vector3.zero, Quaternion.identity) as GameObject;
    14.      }
    15.      void Update()
    16.      {
    17.          RaycastHit hit;
    18.          if (Physics.Raycast(transform.position, transform.forward, out hit, 7f, lm))
    19.          {
    20.              if(!bh.activeInHierarchy)
    21.                  bh.SetActive(true);
    22.              Vector3 worldBlockP = hit.point - (hit.normal / 2);
    23.              Vector3 worldBlockAddP = hit.point + (hit.normal / 2);
    24.              Vector3 blockPos = new Vector3(Mathf.FloorToInt(worldBlockP.x), Mathf.FloorToInt(worldBlockP.y), Mathf.FloorToInt(worldBlockP.z));
    25.              Vector3 blockAddPos = new Vector3(Mathf.FloorToInt(worldBlockAddP.x), Mathf.FloorToInt(worldBlockAddP.y), Mathf.FloorToInt(worldBlockAddP.z));
    26.              bh.transform.position = blockPos;
    27.              if (Input.GetMouseButtonDown(1))
    28.              {
    29.                  Chunk c = Chunk.SetWorldBlock(blockAddPos, 3);
    30.                  if (c != null)
    31.                  {
    32.                      c.dirty = true;
    33.                  }
    34.                  }
    35.              if (Input.GetMouseButtonDown(0))
    36.              {
    37.                  byte b = Chunk.GetWorldBlock(blockPos);
    38.                  if (b > 0)
    39.                  {
    40.                      Block selectedBlock = Block.blocks[b - 1];
    41.                      if (selectedBlock.name == "bedrock")
    42.                      {
    43.                          return;
    44.                      }
    45.                  }
    46.                  Chunk c = Chunk.SetWorldBlock(blockPos, 0);
    47.                  if (c != null)
    48.                  {
    49.                      c.dirty = true;
    50.                      Vector3 localPos = blockPos - c.chunkPos;
    51.                      if(localPos.x == 0)
    52.                      {
    53.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(-Chunk.chunkWidth, 0, 0));
    54.                          if (nC != null)
    55.                          {
    56.                              nC.dirty = true;
    57.                              nC.dirtyCollider = true;
    58.                          }
    59.                      }
    60.                      if (localPos.x == Chunk.chunkWidth - 1)
    61.                      {
    62.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(Chunk.chunkWidth, 0, 0));
    63.                          if (nC != null)
    64.                          {
    65.                              nC.dirty = true;
    66.                              nC.dirtyCollider = true;
    67.                          }
    68.                      }
    69.                      if (localPos.z == 0)
    70.                      {
    71.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, -Chunk.chunkWidth));
    72.                          if (nC != null)
    73.                          {
    74.                              nC.dirty = true;
    75.                              nC.dirtyCollider = true;
    76.                          }
    77.                      }
    78.                      if (localPos.z == Chunk.chunkWidth - 1)
    79.                      {
    80.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, Chunk.chunkWidth));
    81.                          if (nC != null)
    82.                          {
    83.                              nC.dirty = true;
    84.                              nC.dirtyCollider = true;
    85.                          }
    86.                      }
    87.                      if (localPos.y == 0)
    88.                      {
    89.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, -Chunk.chunkHeight, 0));
    90.                          if (nC != null)
    91.                          {
    92.                              nC.dirty = true;
    93.                              nC.dirtyCollider = true;
    94.                          }
    95.                      }
    96.                      if (localPos.y == Chunk.chunkHeight - 1)
    97.                      {
    98.                          Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, Chunk.chunkHeight, 0));
    99.                          if (nC != null)
    100.                          {
    101.                              nC.dirty = true;
    102.                              nC.dirtyCollider = true;
    103.                          }
    104.                      }
    105.                  }
    106.              }
    107.          } else
    108.          {
    109.              bh.SetActive(false);
    110.          }
    111.      }
    112. }