4 Replies - 297 Views - Last Post: 21 December 2014 - 08:56 AM Rate Topic: -----

#1 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 160
  • Joined: 15-February 11

UNITY - test if touch hits object

Posted 19 December 2014 - 02:58 PM

Hi, i'm new to Unity and trying to test if touch hits game object. This script is attached to my moveable object. I'm getting a runtime error (with no description, just a pointer to line in assembly code)

void Update () 
	{
		RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
		
		if (hit.collider.gameObject.name == "mask")
		{
						if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) 
						{
								sceneStart = false;
								isDragged = true;
								offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
						}
		
						if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) 
						{
								Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
								Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
								transform.position = curPosition;
						}
		
						if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) 
						{
								isDragged = false;
								if (isCollided) 
								{
										correctAudio.Play ();
										targetPosition = maskPosition;
										Debug.Log ("Is in mask.");
								} 
								else 
								{
										errorAudio.Play ();
										targetPosition = initPosition;
										Debug.Log ("Not in mask.");
								}

						}
		}
		if (!isDragged && !sceneStart) 
		{
			Vector3 direction = targetPosition - transform.position;
			if (direction.magnitude > threshold) 
			{
				direction.Normalize ();
								transform.position = transform.position + direction * speed * Time.deltaTime;
						} else { 
								transform.position = targetPosition;
						}
		}
	}



The error comes from my line
if (hit.collider.gameObject.name == "mask")



What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: UNITY - test if touch hits object

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,322
  • Joined: 24-April 12

Re: UNITY - test if touch hits object

Posted 20 December 2014 - 04:02 AM

On line 03, how does this make sense?

 Physics2D.Raycast(..., Vector2.zero); 



Shouldn't the second parameter be a direction? How does a zero "vector" make sense?

I'm looking at the Unity docs and the example has:

RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);



Although that's a bit weird in itself because why would you ever use a negative up vector? Wouldn't Vector2.down suffice and be more clear?

I have not used this before; so I don't really know how this works, but just looking at the doc it doesn't seem to match up with what you've got going on there. Plus, there's not really any such thing as a zero vector although mathematicians and programmers may argue with me on that one. Zero vectors are basically undefined.

I was just going over this yesterday. When a vector is zero'ed, it ceases to exist. A zero vector is basically the absence of a vector. You have to be careful when working with zero vectors because the results are likely undefined. For example, what is the length/magnitude of a zero'ed vector? Because it doesn't exist, the question is like "What is the sound of one hand clapping?" The problem is in the question. If you do the math, the length of a zero vector involves a divide by zero, which is also undefined and will give an error.

You almost never want a zero vector for anything. Zero vectors are generally bad. I'm not even completely sure why they were included in the language. I'm sure there's an occasion where they serve a purpose, but I would avoid them as much as possible.

But here, the parameter is a direction. A zero vector is never a direction. It's not anything. It's the absence of something, or the absence of a vector.

Anyway, that looks wrong to me.

But then the next thing is that ScreenToWorldPoint returns a Vector3 according to the doc. I don't see anything about returning a Vector2. There seems to be serious confusion here as to whether we're working in 2D or 3D. Can you not just use the mouse position? I could be wrong on that, since I've never done this, but that looks wrong to me.
Was This Post Helpful? 0
  • +
  • -

#3 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 160
  • Joined: 15-February 11

Re: UNITY - test if touch hits object

Posted 20 December 2014 - 06:07 AM

Yes, I took Linear Algebra too ;) Programming is not pure math, compilers can be tricked into doing things.... Anyway the error stems from another line altogether.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,322
  • Joined: 24-April 12

Re: UNITY - test if touch hits object

Posted 21 December 2014 - 06:28 AM

You said that the error is "assembly code" of line 5. That line doesn't do anything except branch based on the condition of equality between a string ("mask") and an attribute of the hit object. The hit object is defined in the code I mentioned. If it is defined improperly, it is reasonable to assume it would cause a problem later when an attempt to use it occurs. Although, you would think it would give a more graceful error message.

Outside of that, it sounds like a bug in Unity and you need to completely uninstall Unity and reinstall it. I would thoroughly examine line 3 for its effect on line 5 first though.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: UNITY - test if touch hits object

Posted 21 December 2014 - 08:56 AM

You need to check if anything was hit before attempted to get any information. Since you are basically raycasting every frame then hit.collider could be null quite often.

From the docs

Quote

This function returns a RaycastHit object with a reference to the collider that is hit by the ray (the collider property of the result will be NULL if nothing was hit).


So essentially check if hit.collider != null before attempting anything.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1