Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 25, 2018 at 12:32 PM by PaincideStudio for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PaincideStudio · Jan 23, 2018 at 03:52 PM · androidbuttonraycasting

Raycast using button

Hi.
I'm trying to make an Android FPS Game.
I'm know how to use Raycasting. But When I'm trying to cast using a Button(Made on the Canvas), it doesn't seems to work.
I'm making a script that opens when I press a button.
I'm sure that nothing is wrong, but dunno why it doesn't work.
Here is the script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class OpenDoor : MonoBehaviour {
 
     public Camera cam;
     public Animator door;
     bool isOpened;
 
     // Use this for initialization
     void Start () 
     {
         isOpened = false;
     }
     
     // Update is called once per frame
     public void OnClick ()
     {
         if (isOpened = false) {
             Ray ray = new Ray (cam.transform.position, cam.transform.forward);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, 5.0f)) 
             {
                 if (hit.collider.CompareTag ("WarehouseDoor")) 
                 {
                     door.SetBool ("Open", true);
                     isOpened = true;
                 }
             }
         }
     }
 }
 
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jan 23, 2018 at 03:57 PM 0
Share

Have you tried to put simple Debug.Log and Debug.DrawRay inside your various conditions to check where is the exact problem?

avatar image PaincideStudio Hellium · Jan 23, 2018 at 04:30 PM 0
Share

Yep. It doesn't raycast. Should I use void Update and onbuttondown ins$$anonymous$$d of public void OnClick? I'm adding a photo. alt text

1.png (46.2 kB)
avatar image Hellium PaincideStudio · Jan 24, 2018 at 04:37 PM 0
Share

Are you sure the click is detected? Does the following code outputs something when you click on the button?

  public void OnClick ()
  {
      Debug.Log( isOpened ) ;
      if (isOpened == false)
      {
          // ....
      }
  }

I've just tested your script, works fine here. ($$anonymous$$ake sure your door is close enough to your camera, otherwise, the Ray won't hit)

Show more comments
avatar image Hellium · Jan 23, 2018 at 09:33 PM 0
Share

The problem may come from here:

 if (isOpened = false)

Should be :

 if (isOpened == false)

Or

 if (!isOpened)
avatar image PaincideStudio Hellium · Jan 24, 2018 at 03:42 PM 0
Share

Nope. Doesn't work. I already checked for all of my Animators and Cameras. Any Idea?

avatar image Hamburgert PaincideStudio · Jan 24, 2018 at 07:20 PM 0
Share

This is the correct source of the problem. Single equation character means the if will always fail.

if(isOpened = false) // always fails if(isOpened == false) // Fails only when isOpened is true

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

224 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get access to bool from one script to another with touch input? 1 Answer

How to disable back button in android during unity splash screen display? 0 Answers

How can I make a UI button press affect something in a text box? 0 Answers

Someone please help me with combination lock 0 Answers

Is it possible to take input from 2 UI buttons? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges