- Home /
[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); } } }
So, you want to touch the 2d object (to highlight it), then touch somewhere else to move it?
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.
If you want to hit a certain sprite, I can help with that.
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
}
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.
Thank you so much, this was so much simpler than I thought!
Your answer
Follow this Question
Related Questions
Raycasting not behaving as expected? 2 Answers
Pixel perfect and raycasting 0 Answers
Get information on ignored collision 1 Answer