Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Knowing which side of a box collider the mouse is clicking

Discussion in 'Scripting' started by TommyLP, Feb 24, 2015.

  1. TommyLP

    TommyLP

    Joined:
    Feb 24, 2015
    Posts:
    4
    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!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Mycroft likes this.
  3. TommyLP

    TommyLP

    Joined:
    Feb 24, 2015
    Posts:
    4
    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?
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.
     
  5. TommyLP

    TommyLP

    Joined:
    Feb 24, 2015
    Posts:
    4
    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?
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    What line is the error occurring on? And what is that line in your code?*

    * - This is always pertinent and useful information.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Camera.current is not == Camera.main
    usually you will want to use main
     
  8. TommyLP

    TommyLP

    Joined:
    Feb 24, 2015
    Posts:
    4
    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:DoSendMouseEvents(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


    }


    }
    }
     
  9. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I'm still betting on camera.current
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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. :)

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