Knowing which side of a box collider the mouse is clicking

Hi Unity Forums,

I am making a voxel based map, and would like to know how to place blocks in a similar way to Minecraft.

Each block has a box collider, as having a mesh collider on each face causes performance issues. When we want to place a new block, we need to find out which side of the box collider was clicked, so that we can place the new block in the right position.

Does anyone have any ideas?

Thanks!

hit.normal

will point in the direction of the next block to place

1 Like

Thanks for the reply,

When loading this into our game, it complains because we do not have an object called gunObj. What should this be/what does this object do?

In that example, gunObj is the player’s weapon, or the origin point of the raycast. If you’re not going to use the gunObj object from the sample, use something like your main camera.

I’ve changed “gunObj” to “Camera.current.transform.position” in the code. My code is now:

if (Input.GetMouseButtonDown(1))

{

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Vector3 incomingVec = hit.point - Camera.current.transform.position;
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
Debug.DrawLine(Camera.current.transform.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
}
}

I have added this script to the blocks we have. When right clicking on blocks, I get the error message: “NullReferenceException: Object Reference not set to an instance of an object”.

What could be causing this issue?

What line is the error occurring on? And what is that line in your code?*

    • This is always pertinent and useful information.

Camera.current is not == Camera.main
usually you will want to use main

Sorry for not getting back to you, I’ve had a lot of work to do for uni and haven’t gotten around to trying to fix this.

The error I get is:
NullReferenceException: Object reference not set to an instance of an object
ClickOnFaceScript.OnMouseOver () (at Assets/Code/ClickOnFaceScript.cs:51)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

So I guess that’s line 51 of this:

/* Andrei Jifcovici

  • In2GPU.com
    */
    using UnityEngine;
    using System.Collections;

public class ClickOnFaceScript : MonoBehaviour
{
// Public fields are visible and their values can be changed dirrectly in the editor
// represents the position displacement needed to compute the position of the new instance
public Vector3 delta;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
Screen.lockCursor = true;
}

// This function is triggered when the mouse cursor is over the GameObject on which this script runs
void OnMouseOver()
{
// If the left mouse button is pressed
if (Input.GetMouseButtonDown(0))
{
// Display a message in the Console tab
//Debug.Log(“Left click!”);
// Destroy the parent of the face we clicked
Destroy(this.transform.gameObject);
//Destroy(this.transform.parent.gameObject);

}

// If the right mouse button is pressed
if (Input.GetMouseButtonDown(1))

{

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Vector3 incomingVec = hit.point - Camera.current.transform.position;
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
Debug.DrawLine(Camera.current.transform.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
}

// Display a message in the Console tab
//Debug.Log(“Right click!”);

// Call method from WorldGenerator class
//WorldGenerator.CloneAndPlace(this.transform.gameObject.transform.position + delta, // N = C + delta
//this.transform.gameObject); // The parent GameObject

}

}
}

@TommyLP
Please do us all a favour :

I’m still betting on camera.current

Yeah, always make sure to wrap your code in code tags to make it readable to those trying to help you. Also gives us a chance at figuring out which line 51 is. :slight_smile:

Edit: As @hpjohn said, it looks like the issue is with camera.current.