detect if gameobject is clicked/touched

Hi Guys,

im working on a little game where the user have to touch a button to let an object jump against the gravity. Also he can touch the screen to shoot an alser to this psotion. My problem is, i cant get it working that when the jumpbutton is touched, the laser isn fired. If you tap or click the button, the laser is fired and the player jumps. If you tap somewhere else on the screen, the alser is fired but the player doesn’t jump.

The function it should have: tap the button and palyer jumps but no laser is fired. Tap somewhere else and the laser is fired but no jump is executed.

Here is my weapon script for the lasershooting, as this seems to be the problem:

using UnityEngine;
using System.Collections;

public class WeaponScript : MonoBehaviour {

	public LineRenderer laser;
	private GameObject flybutton;
	private GameObject lasers;

	// Use this for initialization
	void Start () {
		flybutton = GameObject.Find ("Tap_to_fly");
		lasers = GameObject.Find ("laserbeam");
	}
	
	// Update is called once per frame
	void Update () {
	
		if (Application.platform == RuntimePlatform.Android)
		{
			if (Input.touchCount > 0)
			{

				if (Input.GetTouch(0).phase == TouchPhase.Began)
				{ 
					CheckTouch(Input.GetTouch(0).position, "began"); // function created below


				} else if (Input.GetTouch(0).phase == TouchPhase.Ended)
				{
					CheckTouch(Input.GetTouch(0).position, "ended");
				}
			}
		}
		
		/* Check if the user is touching the button on the Editor, change OSXEditor value if you are on Windows */
		
		if (Application.platform == RuntimePlatform.WindowsEditor)
		{

			if (Input.GetMouseButtonDown(0))
			{
				CheckTouch(Input.mousePosition, "began");

			}
			
			if (Input.GetMouseButtonUp(0))
			{
				CheckTouch(Input.mousePosition, "ended");
			}
		}
	}

	void CheckTouch(Vector3 pos, string phase)
	{
		/* Get the screen point where the user is touching */
		Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
		Vector2 touchPos = new Vector2(wp.x, wp.y);
		Vector2 weaponPos = new Vector2(lasers.transform.position.x, lasers.transform.position.y);
		Vector2 buttonPos = new Vector2 (flybutton.transform.position.x, flybutton.transform.position.y);
		// Collider2D hit = Physics2D.OverlapPoint(touchPos);
		
		/* if button is touched... */
		
		if (touchPos == buttonPos && phase == "began") {
			Vector2 zero = new Vector2(0,0);
			laser.SetPosition(0,zero);
			laser.SetPosition(1,zero);	
				}
		else if (phase == "began")
		{
			laser.SetPosition(0,weaponPos);
			laser.SetPosition(1,touchPos);
		}
		if (phase == "ended") {
			Vector2 zero = new Vector2(0,0);
			laser.SetPosition(0,zero);
			laser.SetPosition(1,zero);	
		}
	}
}

Not sure what you want. You've commented out the only code that would check if something is hit by the touch: // Collider2D hit = Physics2D.OverlapPoint(touchPos); You need some code that check if the touch hits the object that you want to make jump instead.

Does this happen in android, in PC, or both ?

3 Answers

3

Hey, if you want to detect if a gameObject is clicked just copy and paste this to your script:

void OnMouseEnter()
{
if(Input.GetMousebuttonDown(0)){
//now your gameObject was clicked!
DoSomething();
}
}

By experience the problem with the above is, when u have a need to detect simultaneous touches on different objects. Though I only tried this in the context of the void Update() function.

Please, tell us where is your CheckTouch function call. If you just attach collider to your game object then it haven’t help you. And so you have to call your CheckTouch from OnMouseDown call on the same script. Sorry my bad english.

Oh i think i know what you mean. Is it possible to determine a specific gameObject Collider in the script or is Collider2d always using the gameobject where the script is attached to? because i attached that script to my weapon gameobject and not to the button gameobject.

Your solution is attach collider to the weapon. Exactly. So you can tap to the weapon and catch OnMouseDown in weapon script.

but i don't want to detect if the weapon is tapped, i want to detect if the button is tapped and when then jump and don't shoot the laser else only shoot the laser.

So i reworked it and put both scripts, the weaponscript and the flyingscript together to one PlayerControl Script. But it still got the same issue. I tap the button and my Playerobject flys, i tap somewhere else and nothing happens. The point is, if i tapp somewhere else it should fire a laser by using a linerenderer.

The Script is attached to the button which si sued to fly the player. The Renderer component is also attached to the button. The laserbeam gameobject is attached to the palyerobject. The laserbeam is only a empty object, i only use it to get the start position of the laserbeam.

here is the complete script:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

	
	public float flyforce;
	private GameObject player;
	public LineRenderer laser;
	private GameObject flybutton;
	private GameObject lasers;

	// Use this for initialization
	void Start () {
		player = GameObject.Find ("Player");
		lasers = GameObject.Find ("laserbeam");
	}
	
	// Update is called once per frame
	void Update () {
		/* Check if the user is touching the button on the device */
		
		if (Application.platform == RuntimePlatform.Android)
		{
			if (Input.touchCount > 0)
			{
				if (Input.GetTouch(0).phase == TouchPhase.Began)
				{ 
					CheckTouch(Input.GetTouch(0).position, "began"); // function created below
				} else if (Input.GetTouch(0).phase == TouchPhase.Ended)
				{
					CheckTouch(Input.GetTouch(0).position, "ended");
				}
			}
		}
		
		/* Check if the user is touching the button on the Editor, change OSXEditor value if you are on Windows */
		
		if (Application.platform == RuntimePlatform.WindowsEditor)
		{
			if (Input.GetMouseButtonDown(0))
			{
				CheckTouch(Input.mousePosition, "began");
			}
			
			if (Input.GetMouseButtonUp(0))
			{
				CheckTouch(Input.mousePosition, "ended");
			}
		}
	}
	
	void CheckTouch(Vector3 pos, string phase)
	{
		/* Get the screen point where the user is touching */
		Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
		Vector2 touchPos = new Vector2(wp.x, wp.y);
		Vector2 weaponPos = new Vector2(lasers.transform.position.x, lasers.transform.position.y);
		Collider2D hit = Physics2D.OverlapPoint(touchPos);
		
		/* if button is touched... */
		
		if (hit.gameObject.name == "Tap_to_fly" && hit && phase == "began") {
			player.rigidbody2D.velocity = new Vector2 (0f, flyforce); //Add jump force to hero

		} 

		else if(phase == "began") {
			laser.SetPosition(0,weaponPos);
			laser.SetPosition(1,touchPos);
		}
		Vector2 zero = new Vector2(0,0);
		laser.SetPosition(0,zero);
		laser.SetPosition(1,zero);
	}
}