vaso
vaso

Reputation: 39

Detecting touch on a specific object in Unity

working on an android game in Unity 2D. Long story short, I have two separate objects in the scene which have this script attached on:

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

public class SwipeScript : MonoBehaviour
{
    Vector2 startPos, endPos, direction;
    float touchTimeStart, touchTimeFinish, timeInterval;
    public static int brojbacanja=0;
    public static bool bacenaprva = false;

    [Range (0.05f, 1f)]
    public float throwForce = 0.3f;

    void Update(){
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began && brojbacanja == 0) {

                    touchTimeStart = Time.time;
                    startPos = Input.GetTouch (0).position;

        }
            if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended && brojbacanja == 0) {
                touchTimeFinish = Time.time;
                timeInterval = touchTimeFinish - touchTimeStart;
                endPos = Input.GetTouch (0).position;
                direction = startPos - endPos;
                GetComponent<Rigidbody2D> ().AddForce (-direction / timeInterval * throwForce);
                brojbacanja = 1;
                bacenaprva = true;
            }
    }
}

What this does is pretty much alowing me to swipe anywhere on the screen and throw an object which it's attached to. Since I have two different objects which I would like to throw separately, I want to change it a bit so when I touch object which I want to throw, other one stays still. I read about this problem alredy and tried using Raycast and OnMouseDown() but didn't know how to implement it.

If anyone can help, would be grateful.

Upvotes: 0

Views: 3623

Answers (1)

Ian Pilipski
Ian Pilipski

Reputation: 535

Since this script will be on each object in the scene you will need to have an extra variable and a check to see if the initial touch is intersecting your object.

If you are using 3D objects (which I am assuming because you mentioned ray tracing), then you will want something that converts the touch position on the screen to a ray, that you then cast to see if it intersects your object. Your object will have to have collision for this to work.

public class SwipeScript : MonoBehaviour
{
    // added these two values, set the coll value to your collider on this object.
    bool isTouching;
    public Collider coll;
 
    Vector2 startPos, endPos, direction;
    float touchTimeStart, touchTimeFinish, timeInterval;
    public static int brojbacanja=0;
    public static bool bacenaprva = false;

    [Range (0.05f, 1f)]
    public float throwForce = 0.3f;

    void Update(){
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began && brojbacanja == 0) {
                if(IsTouchOverThisObject(Input.GetTouch(0))) {
                    isTouching = true;
                    touchTimeStart = Time.time;
                    startPos = Input.GetTouch (0).position;
                }

        }
            if (isTouching && Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended && brojbacanja == 0) {
                isTouching = false;
                touchTimeFinish = Time.time;
                timeInterval = touchTimeFinish - touchTimeStart;
                endPos = Input.GetTouch (0).position;
                direction = startPos - endPos;
                GetComponent<Rigidbody2D> ().AddForce (-direction / timeInterval * throwForce);
                brojbacanja = 1;
                bacenaprva = true;
            }
    }

    bool IsTouchOverThisObject(Touch touch) {
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
         RaycastHit hit;

         // you may need to adjust the max distance paramter here based on your
         // scene size/scale.
         return coll.Raycast(ray, out hit, 1000.0f); 
    }
}

Upvotes: 1

Related Questions