- Home /
[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;
}
}
}
Answer by LCStark · Dec 21, 2018 at 06:52 AM
Use Physics2D.Raycast for 2D objects.
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.
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);
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
Follow this Question
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