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 3kWikiGames · Dec 21, 2018 at 06:47 AM · 2draycasttouchtagspickup

[Problem] Cant activate 2D objects with touch?

So I'm currently creating a game that requires that I can tap on 2D sprites to either pick them up or activate them. My scripting works for 3D objects but for some reason doesn't seem to work with 2D objects. Using raycasts currently but will rework my coding so that I can work with 2D. Thank you so much for your help!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class Interactables: MonoBehaviour
 {
     Ray ray;
     RaycastHit hit;
 
     private float distp;
     public float maxdist;
 
     private bool ispaused;
     bool open = false;
 
     public static String trigname;
     public static bool radiohit = false;
     private Vector3 v1;
 
     private int hitnum = 0;
 
     // Use this for initialization
     void Start()
     {
         maxdist = 5f;
     }
 
     // Update is called once per frame
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Clickers();
         ispaused = CharacterSettings.paused;
         if (open == true)
         {
             //close after far away enough
         }
     }
 
     private void Clickers()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(ray, out hit, 100.0f))
             {
                 if (hit.transform != null)
                 {
                     //Debug.Log(hit.transform.tag);
                     if (ispaused == false)
                     {
                         distp = Vector3.Distance(transform.position, hit.transform.position);
                         //Here goes the selectable items
                         if (distp <= maxdist)
                         {
                             Note();
                             Door();
                             Battery();
                             conversation();
                             radio();
                         }
                     }
                 }
             }
         }
     }
 
     private void Note()
     {
         if (hit.transform.tag == "Note")
         {
             //Activate Note Trigger
         }
     }
 
 
     private void Door()
     {
         if (hit.transform.tag == "Door")
         {
             if (open == false)
             {
                 //hit.transform.parent.transform.Rotate(0.0f, 120.0f, 0.0f);
                 hit.transform.parent.transform.rotation = Quaternion.RotateTowards(hit.transform.parent.transform.rotation, new Quaternion(0.0f, 10.0f, 0.0f, 0.0f), 10 * Time.deltaTime);
                 open = true;
             }
             else if(open == true)
             {
                 hit.transform.parent.transform.Rotate(0.0f, -120.0f, 0.0f);
                 open = false;
             }
         }
     }
 
     private void Battery()
     {
         if (hit.transform.tag == "Battery")
         {
             Flashlight.battery++;
             Destroy(hit.transform.gameObject);
         }
     }
 
     private void conversation()
     {
         //Use Dialogue Trigger
     }
 
 
     private void radio()
     {
         if (hit.transform.name == "Radio" && hitnum == 0)
         {
             hitnum = 1;
             hit.transform.GetComponent<AudioSource>().Play();
         }
         else if (hitnum == 1)
         {
             hit.transform.GetComponent<AudioSource>().Pause();
             hitnum = 2;
         }
         else if (hitnum == 2)
         {
             hit.transform.GetComponent<AudioSource>().UnPause();
             hitnum = 1;
         }
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LCStark · Dec 21, 2018 at 06:52 AM

Use Physics2D.Raycast for 2D objects.

Comment
Add comment · Show 3 · 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
avatar image 3kWikiGames · Dec 21, 2018 at 07:09 AM 0
Share

Thank you so much, Im not very familiar with Physics2D.Raycasts, is there a simple way to arrange my code so that I could incorporate that or will it require quite a bit of research and reworking of code.

avatar image LCStark 3kWikiGames · Dec 21, 2018 at 07:23 AM 0
Share

It shouldn't require a lot of changes to your code. The main difference is that Physics.Raycast returns a bool value (true on collission / false on no collision) and Physics2D.Raycast doesn't, so ins$$anonymous$$d of:

if (Physics.Raycast()) {}
you'd write:
RaycastHit2D hit = Physics2D.Raycast();
if (hit.collider != null) {}
Also note that the 2D version doesn't take the Ray object as a parameter, you have to pass its origin and direction components manually:
hit = Physics2D.Raycast(ray.origin, ray.direction);
avatar image 3kWikiGames LCStark · Dec 21, 2018 at 07:42 AM 0
Share

Awesome Ill keep working around with it but I think im headed in the right direction, ill post an update when I get it working. Thanks again!

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

241 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 avatar image avatar image avatar image avatar image avatar image 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

Inaccurate Touch Detection iOS 1 Answer

Detect touched 2d object in Unity3d 4.3 1 Answer

Problem With Raycast Pickup Using Touch 0 Answers

Raycast Touch Android not working JS 2 Answers

2d raycasting to touch position not working 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