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 /
avatar image
0
Question by NinjaRubberBand · Dec 08, 2016 at 03:20 PM · 2dtouchcontrol

Touchinput script converted from 3d to 2d

I tried converting this script:

 public class TouchInput : MonoBehaviour {
   
       public LayerMask touchInputMask;
       
       private List<GameObject> touchList = new List<GameObject>();
       private GameObject[] touchesOld;
       private RaycastHit hit;
       
       // Update is called once per frame
       void Update () {
           
           
   #if UNITY_EDITOR
       
       if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0)  || Input.GetMouseButtonUp(0)) {
           
           touchesOld = new GameObject[touchList.Count];
               touchList.CopyTo(touchesOld);
               touchList.Clear();
    
           
           
                     Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                   
                     
                     if(Physics.Raycast(ray,out hit,touchInputMask)) {
                         
                         GameObject recipient = hit.transform.gameObject;
                         touchList.Add(recipient);
                         
                         if(Input.GetMouseButtonDown(0)) {
                             recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                             
                         }  if(Input.GetMouseButtonDown(0)) {
                             recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                             
                         }  if(Input.GetMouseButtonUp(0)) {
                             recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                             
                         }  
                   
       
                       }
                    
                    foreach (GameObject g in touchesOld) {
                        if(!touchList.Contains(g)) {
                            g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                        }
                        
                    }
           
               }
       
   #endif
           
           
           
           
           
       
           }
   }
 
 
 

To 2d, where i got this script.

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  
  public class TouchInput : MonoBehaviour {
  
      public LayerMask touchInputMask;
      
      private List<GameObject> touchList = new List<GameObject>();
      private GameObject[] touchesOld;
      public RaycastHit hit;
     public GameObject recipient;
      
      // Update is called once per frame
      void Update () {
          
          
  #if UNITY_EDITOR
      
      if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0)  || Input.GetMouseButtonUp(0)) {
          
          touchesOld = new GameObject[touchList.Count];
              touchList.CopyTo(touchesOld);
              touchList.Clear();
   
          
          
                    //Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                  
                    
                   if (Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero))  {
                        
                       recipient = hit.transform.gameObject;
                        touchList.Add(recipient);
                        
                        if(Input.GetMouseButtonDown(0)) {
                            recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  if(Input.GetMouseButtonDown(0)) {
                            recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  if(Input.GetMouseButtonUp(0)) {
                            recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  
                  
      
                      }
                   
                   foreach (GameObject g in touchesOld) {
                       if(!touchList.Contains(g)) {
                           g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                       }
                       
                   }
          
              }
      
  #endif
          
          
          
          
          
      
          }
  }




i Changed if(Physics.Raycast(ray,out hit,touchInputMask)) { to if(Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero)) {

and deleted the ray variable.

I change the layer of my touch object to touchInputMask, and attach a script that says: void OnTouchDown () {

It works fine unity i play, and press on the 2d object (Which is a moving object, and when i tap it it needs to be destroyed) and it then says "cant find instance of object" something like that.

Its because it doesn't know what the variable "hit" is... How do i change the script to 2d, so it knows what hit is?

Comment
Add comment · Show 1
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 edcx · Dec 08, 2016 at 04:05 PM 0
Share

In your another question, I suggested you to check the documentation of raycast2d and catching hit. You are asking same question again!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Dec 08, 2016 at 06:17 PM

The Physics2D.Raycast does not return a bool like the 3D version does, but the RaycastHit2D directly. If you look at the documentation here there are 2 important lines:

         RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
         if (hit.collider != null) {...

adapt your version with this and you should have your "hit" again.

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Move horizontally on touch 0 Answers

Ui sidescroller 2d touch ? 0 Answers

How to detect a touch on a 2D sprite 2 Answers

Simultaneous Touch Drag Controls 0 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