0

I'm actually working on a word search like game.

But i'm having issues when i want to select instantiate gameObjects on a grid.

as i saw on unity doc, i used raycast to check which collider has been hit, and do some logic.

So i wrote this:

void Update(){

    if (Input.GetMouseButtonDown(0))
    {
        Ray ray;
        RaycastHit hit;
        GameObject current;

        ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        if (Physics.Raycast (ray, out hit))
        {
            if (hit.collider.tag == "tile")
            {
                //Do some logic
            }
        }
    }

After that, i used this:

Vector3 forward = transform.TransformDirection(Vector3.forward) * 50;
Debug.DrawRay(transform.position, forward, Color.green);

to display raycasts when i run the game.

So when i click with my mouse, a single ray is fired.

Normally it should fire a lot more. But i don't know why it's shooting just one ray.

Also,

Input.mousePosition

does not seem to work. the ray just fires at the center of my screen.

How can i fix that? Is there other method than raycast?

Thanks a lot.

Samih EL SAKHAWI
  • 429
  • 4
  • 17
  • You just want to detect which object is clicked? What type of object is this(2D sprite, 3D mesh or UI)? – Programmer Mar 05 '18 at 18:14
  • I want to detect when the user touch the screen to select some letters. To make a word, the player need to maintain the touch and move is finger to other letters. Letters are UI elements. – Samih EL SAKHAWI Mar 05 '18 at 18:17
  • And those letters are the objects you are raycasting? Is this a Text or Image component? – Programmer Mar 05 '18 at 18:18
  • Yes, i cast ray from the camera, and normally when it hits a letter, it should do something, but the raycast just fires one ray, and stop after that. i don't have any error on my log. – Samih EL SAKHAWI Mar 05 '18 at 18:20
  • 1
    `Physics.Raycast` is used on 2D or 3D objects not UI's. Check #1 from the duplicate on ways to detect clicks on UI components. Also, please don't attach a collider to a UI component – Programmer Mar 05 '18 at 18:24
  • So, how could i detect a moving finger on a UI element? – Samih EL SAKHAWI Mar 05 '18 at 18:29
  • 1
    That would be `OnDrag` in this case. See the duplicate for how to use that. – Programmer Mar 05 '18 at 18:33
  • 1
    Aside of that programmer is right about UI elements, what appears to be only one ray firing is actually you drawing the wrong thing. To properly draw your ray use `Debug.DrawRay(ray.origin, ray.direction * 10f, Color.green);` (its multiplied by 10, because the direction is normalized and not the distance to the hit, the number is arbitrary) - currently you draw a "ray" from the gameobject holding your scripts position along its forward axis, not the ray you created. – yes Mar 05 '18 at 18:33
  • @yes Thanks, after i change what you said, i can see the direction, but it still fires just one ray when i old my mouse button down. – Samih EL SAKHAWI Mar 06 '18 at 16:31
  • There is an overload with a 4th argument for `Debug.DrawRay` its how long the ray is visible (in seconds), by default its drawn for only one frame and thats way to short to see. Also `Input.GetMouseButtonDown` only is true in the frame you pressed the button - maybe you want `Input.GetMouseButton` instead? Its true for as long as the button is pressed. https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html – yes Mar 07 '18 at 07:55

0 Answers0