1
\$\begingroup\$

I made this C# script, becase I want select a Button of the menu(canvas) for choice the dificulty of the game, but I don't want select with mouse I just want select with Aim, and I made this script for make this happen, but don't it works. What am I doing wrong ?

My C# Script:

using UnityEngine;
using System.Collections;

public class AimManager : MonoBehaviour {

    float TimeInterval = 2f;
    float time = 0;
    public UI_cozinha uiCozinha;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            if(time >= TimeInterval)
            {
                if((hit.transform.tag == "ButtonE"))
                {
                    uiCozinha.setDiff(5);
                }if((hit.transform.tag == "ButtonM"))
                {
                    uiCozinha.setDiff(7);
                }
                if ((hit.transform.tag == "ButtonH"))
                {
                    uiCozinha.setDiff(10);
                }
                time = 0;
            }
            else
            {
                time += Time.deltaTime;
            }
        }

    }
}

CardBoard View:

CardBoard View

Normal GameView:

Normal GameView

Image of Menu(canvas): i.stack.imgur.com/MRIbl.png

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Two options: Either the raycast is going in the wrong direction because the game object its attached to is rotated and its forward is not facing the menu or you have not set the tags on your buttons. Try using Debug.Log(hit.transform.name) to see what your ray is hitting if its anything at all. Another problem might be that you check for the right time after shooting the ray. Try swapping the if blocks and shoot a ray when the time is right. \$\endgroup\$
    – Uri Popov
    May 16, 2016 at 9:31
  • \$\begingroup\$ I checked tags and all be right. I will try, 'Debug.Log(hit.transform.name)', thank you for your help. \$\endgroup\$ May 16, 2016 at 9:56
  • \$\begingroup\$ I use 'Debug.Log(hit.transform.name)', and I see that my recast only give me a hit of my aim, I just put layer that ignore raycast, and result like I want. Thank you dude. @UriPopov \$\endgroup\$ May 16, 2016 at 11:44

2 Answers 2

0
\$\begingroup\$

Ok here is my guess. you are using Physics.Raycast that works with 3d object. but I'm guessing you have put 2DBoxColliders on your buttons. Use this instead.

RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.up);
 if (hit.collider)
 {
  if(hit.collider.gameObject.tag == "someTag")
  {
    //do stuff here
  }
 }

Dont forget to add a Physics 2D RayCaster component to your camera.

\$\endgroup\$
1
  • \$\begingroup\$ sorry but isn't what you said, but ty dude. I find the problem, and isn't what you said. thank you for all \$\endgroup\$ May 16, 2016 at 11:06
0
\$\begingroup\$

I Finaly find my Solution.

When I do Debug.Log(hit.transform.name), I can see that only hit in my Aim, so Physics.Raycast() works in the wrong way, I just change my Layer Type to Ignore Raycast, as it is in the picture:

My new LayerType

When I changed the Layer Type, I could see that my Script was right, and works. So I can select a object tag (in my case a button[canvas]) with Physics.Raycast().

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .