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 22, 2018 at 10:49 PM · 2dphysics2draycastingmovetouchscreen

[Solved] How to touch/activate 2D objects?

So i've been trying for a few days to get my code to work by using Physics2D.Raycast to be able to using raycasting for using a touchscreen to activate or touch 2D objects, but with no luck. I was wondering if someone could give me a simple start since I'm still unable to do this without countless errors. Any help is appreciated, Thank you!

This is an example of my code Ive been using so far: 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)) { RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction); if (hit.collider != null) //Changed { //Debug.Log(hit.transform.tag); if (ispaused == false) { distp = Vector3.Distance(transform.position, hit.transform.position); //Here goes the selectable items } if (distp <= maxdist) { Battery(); } } } } private void Battery() { if (hit.collider.GetComponent<Collider2D>().tag == "Battery") { Flashlight.battery++; Destroy(hit.transform.gameObject); } } }

Comment
Add comment · Show 4
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 Vega4Life · Dec 22, 2018 at 11:02 PM 1
Share

So, you want to touch the 2d object (to highlight it), then touch somewhere else to move it?

avatar image 3kWikiGames Vega4Life · Dec 22, 2018 at 11:26 PM 0
Share

Well, mostly I just want to be able to touch and interact with the objects, such as in my battery code, so when I touch on a 2D Sprite it will add a battery to my character and destroy the sprite. Really just need help with getting it so I can deter$$anonymous$$e if I've touched a certain sprite or not.

avatar image Vega4Life 3kWikiGames · Dec 22, 2018 at 11:30 PM 1
Share

If you want to hit a certain sprite, I can help with that.

Show more comments

1 Reply

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

Answer by Vega4Life · Dec 22, 2018 at 11:37 PM

Here is an example to determine if something can be picked up. You were already on the right track with the raycast. Hope it makes sense.


     public class RayCast : MonoBehaviour
     {
         void Update()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Ray rayLocation = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit2D hit = Physics2D.Raycast(rayLocation.origin, rayLocation.direction);
                 if (hit.collider != null)
                 {
                     // We have multiple options to determine - Tags, Layers, Markups (I like markups because they can hold data)
 
                     // Check if what we hit is a collectible
                     Collectible item = hit.transform.GetComponent<Collectible>();
 
                     // OR check if they have an interface of collectible  (Do this or above, not both :))
                     ICollectible item2 = hit.transform.GetComponent<ICollectible>();
                 }
             }
         }
     }
 
 
     // OUR MARKUPS
     public abstract class Collectible : MonoBehaviour
     {
         // Base class from where all our collectibles derive
         // Then we can just see if the collectible script is on it
     }
 
     public interface ICollectible
     {
         // We can add an interface that is completely empty to our scripts if they can be collected
         // Can be empty, or make a script do something special
     }

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 Vega4Life · Dec 22, 2018 at 11:41 PM 0
Share

The items themselves don't need to raycast, you just need to have a script (like above) that's on some gameObject in the scene. Once it detects clicked collectible item, just pass it to the player so you can add it to the inventory. Once added, just destroy it.

avatar image 3kWikiGames · Dec 23, 2018 at 12:03 AM 0
Share

Thank you so much, this was so much simpler than I thought!

avatar image Vega4Life 3kWikiGames · Dec 23, 2018 at 12:12 AM 0
Share

No problem. Good luck!

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

197 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

Related Questions

Raycasting not behaving as expected? 2 Answers

Pixel perfect and raycasting 0 Answers

Get information on ignored collision 1 Answer

Unity 2D - Rigidbody2D Velocity sudden freeze 0 Answers

Mouseover with Raycast2D doesn't work 1 Answer


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