Unity3D checking if a point is inside a game object

DAVIDBALAS1

I am trying to check in Unity if a point(x location of touch,y location of touch) is inside a game object created(Rectangle shaped), if yes, start and spin it around.

I am pretty new to Unity but I tried doing it myself, here is what

    Rigidbody rb;
    float x, y;
    MeshCollider col;
    Vector3 v;
    bool bo = true;
    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        col = rb.GetComponent<MeshCollider>();
    }
void Update()
{
    if (bo != true)
        rb.transform.Rotate(Vector3.back, Time.deltaTime * 200, Space.World);
    if (Input.touchCount == 1)
    {
        x = Input.GetTouch(0).position.x;
        y = Input.GetTouch(0).position.y;
        Debug.Log(x + "DOTS " + y);
        v = new Vector3(x, y, 0);
        if (col.bounds.Contains(v))
            bo = false;
    }

}

My console is not showing anything, also if I type Debug.Log("HELLO"); and I can't pretty much check myself so that is pretty much my code, appreciate any help.

Jerry Switalski

I think you don't get Log due to you are testing on PC, not on mobile device.

In order to check if the touch point is in bounds of gameObject fire a ray from camera:

#if UNITY_EDITOR
        if(Input.GetMouseButtonUp(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#else
        if(Input.touches.Length == 1 && Input.touches[0].phase == TouchPhase.Ended)
        {
            Ray ray =  Camera.main.ScreenPointToRay(Input.touches[0].position);
#endif

            int layerMask = (1 << YOUR_TARGET_LAYER_ID);
            RaycastHit[] hits = Physics.RaycastAll(ray, 100f, layerMask);

            foreach(RaycastHit hit in hits)
            {
                if(hit.collider == col)
                {
                    Debug.Log("Bingo!!");
                    break;
                }
            }
        }

Note that I used precompiler directives to make possible this code work on mobile device and on PC.

YOUR_TARGET_LAYER_ID is a layer id of the target you want to only be resposive to ray cast. This will ensure that if some other object is covering the target, it will be ignored.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to limit the rotation of a game object in unity3d

From Dev

Unity3D async game object instantiation

From Dev

Performance for checking if a point is inside a triangle (3D)

From Dev

How to move a game object in the forwrard direction of an other game object Unity3D

From Dev

Checking for nulls inside object

From Dev

trying to keep a game object within the screen left and right bounds in unity3d

From Dev

Unity3d - How to make method calling from another class that belongs to another game object efficiently

From Dev

How to have a Game Object remove itself from a list? (Unity3d)

From Dev

How can i know touched which game object with Unity3D

From Dev

How to make a Game Object point towards the mouse in Unity? (C#)

From Dev

How to make a Game Object point towards the mouse in Unity? (C#)

From Dev

Checking entries in EditText object inside ListView

From Dev

Checking if all values are null inside an initialised object

From Dev

XNA Tile based Game: Checking collision between 2 objects using their grid position inside a 2D array

From Java

What's the fastest way of checking if a point is inside a polygon in python

From Dev

Jquery Game - how to keep object inside bounds

From Dev

Android with Unity3d Object OnClickListener

From Dev

Object target radius checking in Unity

From Dev

2D game development with Unity3D

From Dev

How to design a game in Unity3D which is viable for patching?

From Dev

How to install Unity3d game engine on Ubuntu

From Dev

Unity3D Quit Android Game with user prompt

From Dev

How to horizontally center a unity3D game in a div?

From Dev

Using a printer to print Unity3D Game statistics

From Dev

Using/handling bitcoin in Unity3d game

From Dev

Unity3d Design game for Multiple screen size

From Dev

In Unity3D game engine, how to make a delay?

From Dev

Unity3D execute code before the game closes

From Dev

How to install Unity3d game engine on Ubuntu

Related Related

  1. 1

    how to limit the rotation of a game object in unity3d

  2. 2

    Unity3D async game object instantiation

  3. 3

    Performance for checking if a point is inside a triangle (3D)

  4. 4

    How to move a game object in the forwrard direction of an other game object Unity3D

  5. 5

    Checking for nulls inside object

  6. 6

    trying to keep a game object within the screen left and right bounds in unity3d

  7. 7

    Unity3d - How to make method calling from another class that belongs to another game object efficiently

  8. 8

    How to have a Game Object remove itself from a list? (Unity3d)

  9. 9

    How can i know touched which game object with Unity3D

  10. 10

    How to make a Game Object point towards the mouse in Unity? (C#)

  11. 11

    How to make a Game Object point towards the mouse in Unity? (C#)

  12. 12

    Checking entries in EditText object inside ListView

  13. 13

    Checking if all values are null inside an initialised object

  14. 14

    XNA Tile based Game: Checking collision between 2 objects using their grid position inside a 2D array

  15. 15

    What's the fastest way of checking if a point is inside a polygon in python

  16. 16

    Jquery Game - how to keep object inside bounds

  17. 17

    Android with Unity3d Object OnClickListener

  18. 18

    Object target radius checking in Unity

  19. 19

    2D game development with Unity3D

  20. 20

    How to design a game in Unity3D which is viable for patching?

  21. 21

    How to install Unity3d game engine on Ubuntu

  22. 22

    Unity3D Quit Android Game with user prompt

  23. 23

    How to horizontally center a unity3D game in a div?

  24. 24

    Using a printer to print Unity3D Game statistics

  25. 25

    Using/handling bitcoin in Unity3d game

  26. 26

    Unity3d Design game for Multiple screen size

  27. 27

    In Unity3D game engine, how to make a delay?

  28. 28

    Unity3D execute code before the game closes

  29. 29

    How to install Unity3d game engine on Ubuntu

HotTag

Archive