Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How can I make touch detection for canvas objects?

Discussion in '2D' started by xkrbl, Jun 1, 2020.

  1. xkrbl

    xkrbl

    Joined:
    Aug 28, 2018
    Posts:
    10
    Hi!
    I'm making a game in Unity 2D, and on the video you can see how it works for now.
    I want to make script that detects colision between arrow and circles, problem is that I made it in canvas and it's not 2D objects but UI images.
    Please help me, because im begginer programmer and I can't do this.
    If you have questions for this, you can ask.

    Here's link do video:


    Here's my ArrowController script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class ArrowController : MonoBehaviour
    {

    public float speed;
    public bool clock;
    public Text scoreText;

    private int score;

    void Start()
    {
    clock = false;
    score = 0;
    SetScoreText();
    }
    void Update()
    {
    if (clock == true)
    {
    transform.Rotate(new Vector3(0, 0, -45) * Time.deltaTime * speed);
    }

    if (clock == false)
    {
    transform.Rotate(new Vector3(0, 0, 45) * Time.deltaTime * speed);
    }

    if (Input.GetMouseButtonDown(0))
    {
    if (clock == true)
    {
    clock = false;
    Debug.Log("Zmiana kierunku na lewy");
    score = score + 1;
    SetScoreText();
    Debug.Log(score);

    }

    else
    {
    clock = true;
    Debug.Log("Zmiana kierunku na prawy");
    score = score + 1;
    SetScoreText();
    Debug.Log(score);
    }
    }

    }

    void SetScoreText ()
    {
    scoreText.text = score.ToString();
    }
    }
     
  2. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    you can check it by the angle of the arrow, if the angle is above -45 and below 45 then you know is in the blue zone and etc for the other colors
     
  3. xkrbl

    xkrbl

    Joined:
    Aug 28, 2018
    Posts:
    10
    Ok that good option but
    Can you help me with script a little bit?
     
  4. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    put a script on your arrow
    Code (CSharp):
    1. public class AnglesScript : MonoBehaviour{
    2.  
    3.  
    4. void Update(){
    5.  
    6. Debug.Log(transform.localEulerAngles);
    7. }
    8.  
    9. }
    and see if it works to give you angles
     
  5. xkrbl

    xkrbl

    Joined:
    Aug 28, 2018
    Posts:
    10
    Thank you buy I did it other way :)