How can i know touched which game object with Unity3D

Nurullah.c

I am using Unity3D, and i programming augmented reality. If i touch the screen and the touched position has one object, I will do something. Actually I want "How can i know touched which game object ?

My code is here:

    using UnityEngine;
using System.Collections;

public class Atak : MonoBehaviour {
    private Animator anim;
    private GameObject kamera;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
        kamera = GameObject.FindGameObjectWithTag ("Kamera");
    }

    // Update is called once per frame
    void Update () {
        RaycastHit dokunma;
        if (Physics.Raycast(kamera.transform.position,Input.mousePosition,10)&&
            dokunma.collider.gameObject.tag=="Oyuncu" && Input.GetMouseButtonDown(0)
        ){

            anim.SetBool ("Bekle", false);
            anim.SetBool ("Saldir", true);
        } else{
            anim.SetBool ("Bekle", true);
            anim.SetBool ("Saldir", false);
        }
    }
}

The editor gives me error:

Use of unassigned local variable'dokunma'

Programmer

You did not assign anything to the dokunma(RaycastHit) variable. Note that you have to pass in the dokunma variable to the Physics.Raycast function with the out keyword so that it will be initialized and result of the raycast will be stored in it.

void Update()
{
    Ray rayCast = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit dokunma;
    if (Physics.Raycast(rayCast, out dokunma, 10) &&
        dokunma.collider.gameObject.tag == "Oyuncu" && Input.GetMouseButtonDown(0)
    )
    {

        anim.SetBool("Bekle", false);
        anim.SetBool("Saldir", true);
    }
    else
    {
        anim.SetBool("Bekle", true);
        anim.SetBool("Saldir", false);
    }
}

It makes more sense to check for touch press first before performing the raycast. Always use the CompareTag function to compare tags. Something below should do it.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray rayCast = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit dokunma;
        if (Physics.Raycast(rayCast, out dokunma, 10))
        {
            if (dokunma.collider.CompareTag("Oyuncu"))
            {

                anim.SetBool("Bekle", false);
                anim.SetBool("Saldir", true);
            }
            else
            {
                anim.SetBool("Bekle", true);
                anim.SetBool("Saldir", false);
            }
        }
    }
}

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 can I know which objects retain an object?

From Dev

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

From Dev

how to limit the rotation of a game object in unity3d

From Dev

How to know which cell of a grid (10x10) I've touched?

From Dev

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

From Dev

how can i know which button is cliked

From Dev

Qt debugging: How can I know which object QCoreApplication::notifyInternal2 is sending a message to?

From Dev

How can I know which folders are created by which program?

From Dev

How can I know which folders are created by which program?

From Dev

How can I output Unity3D debug log?

From Dev

How can I check if the level is loaded in Unity3D

From Dev

How can I load another level in unity3d?

From Dev

When the screen is touched, how do I make the game over?

From Dev

Can I use my Windows Phone as remote for my unity3d game?

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 which link i clicked in an HTML

From Dev

Can I/how do I configure git extensions to show, within windows explorer, which directories have been touched?

From Dev

How do I detect which SKSpriteNode has been touched

From Dev

Pyside: How can i know which thread emitted the signal "finished"?

From Java

How can I know which button was clicked in the notification?

From Dev

How can I know which machine Juju is actually using?

From Dev

How can I know which properties are applicable for MSBuild?

From Dev

How can I know which element in a list triggered an any() function?

From Java

How can I know which radio button is selected via jQuery?

From Dev

How can I know which nodes in a cluster are actual master nodes?

From Dev

How can I know which type of process of linux will use for these?

From Dev

How can I know which child has exited in C?

From Dev

How can I know which tasks are running in the background?

Related Related

  1. 1

    How can I know which objects retain an object?

  2. 2

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

  3. 3

    how to limit the rotation of a game object in unity3d

  4. 4

    How to know which cell of a grid (10x10) I've touched?

  5. 5

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

  6. 6

    how can i know which button is cliked

  7. 7

    Qt debugging: How can I know which object QCoreApplication::notifyInternal2 is sending a message to?

  8. 8

    How can I know which folders are created by which program?

  9. 9

    How can I know which folders are created by which program?

  10. 10

    How can I output Unity3D debug log?

  11. 11

    How can I check if the level is loaded in Unity3D

  12. 12

    How can I load another level in unity3d?

  13. 13

    When the screen is touched, how do I make the game over?

  14. 14

    Can I use my Windows Phone as remote for my unity3d game?

  15. 15

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

  16. 16

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

  17. 17

    How can i know which link i clicked in an HTML

  18. 18

    Can I/how do I configure git extensions to show, within windows explorer, which directories have been touched?

  19. 19

    How do I detect which SKSpriteNode has been touched

  20. 20

    Pyside: How can i know which thread emitted the signal "finished"?

  21. 21

    How can I know which button was clicked in the notification?

  22. 22

    How can I know which machine Juju is actually using?

  23. 23

    How can I know which properties are applicable for MSBuild?

  24. 24

    How can I know which element in a list triggered an any() function?

  25. 25

    How can I know which radio button is selected via jQuery?

  26. 26

    How can I know which nodes in a cluster are actual master nodes?

  27. 27

    How can I know which type of process of linux will use for these?

  28. 28

    How can I know which child has exited in C?

  29. 29

    How can I know which tasks are running in the background?

HotTag

Archive