0
\$\begingroup\$

I am making a snake game. I'm basing it on a basic tutorial game, which does no collision detection, wall checking or different levels.

All snake head, piece, food, even the background is made of Texture2D. I want the head of the snake to detects 2D collisions with them, but Rect.contains isn't working. I'd prefer to detect collisions by onTriggerEnter() for which I need to add BoxCollider to my snakeHead.

\$\endgroup\$
5
  • \$\begingroup\$ I don't understand the question. Are you asking how to check if two rectangles collide? \$\endgroup\$
    – Anko
    Commented Mar 26, 2014 at 10:49
  • \$\begingroup\$ no, not at all. I am asking can we add physics2D components to the Texture2D object? or If something else that I can do for it? \$\endgroup\$ Commented Mar 26, 2014 at 11:26
  • \$\begingroup\$ Is there a reason you can't add BoxCollider2D? You have an entity, you have a texture attached to it, and you know the other thing you need to add. Why not add it? \$\endgroup\$ Commented Mar 28, 2014 at 19:41
  • \$\begingroup\$ How do you draw your Texture2D? If you use sprites then it's an object which ALSO can have a BoxCollider component. \$\endgroup\$
    – okutane
    Commented Jan 21, 2015 at 10:11
  • \$\begingroup\$ Well, What are you people thinking?? Well, the answer I was trying to figure out was indeed because of my unfamiliarity with Unity. I was making Texture with GUI.DrawTexture in OnGUI() Method. Now the problem has been solved. As I didn't had any mentor so just learnt things by just posting stuff and getting answers thanks allot. :) \$\endgroup\$ Commented Jan 21, 2015 at 14:19

4 Answers 4

0
\$\begingroup\$

To get onTriggerEnter events, you need to set the Is Trigger flag to true.

isTrigger flag

\$\endgroup\$
6
  • 1
    \$\begingroup\$ @gamenewdev I have a feeling that did not answer your question.. \$\endgroup\$
    – pek
    Commented Mar 27, 2014 at 14:54
  • \$\begingroup\$ Yeah U have the right feeling :( \$\endgroup\$ Commented Mar 28, 2014 at 6:12
  • 4
    \$\begingroup\$ @gamenewdev, rather than insincerely thanking people who try to answer based on the limited information available from you, you should improve your question to allow better answers to be posted. You might start by explaining why this doesn't answer your question. \$\endgroup\$ Commented Mar 28, 2014 at 19:37
  • \$\begingroup\$ Ok @SethBattin I'll definitely try next time. I am not a native English speaker, also I am totally a bigginer in unity. That's why I was unable to tell you what I am seeking. \$\endgroup\$ Commented Apr 1, 2014 at 13:41
  • 1
    \$\begingroup\$ You wouldn't be adding the box collider etc to a texture2d... You'd be adding it to a GameObject that has a texture2d... You are creating a snake game that grows when you eat food? When your snake eats good you procedurally create a new body node and you want to know how to add a collider and rigidbody to that?? Is that what your actual question is? \$\endgroup\$
    – Savlon
    Commented Apr 26, 2014 at 3:49
3
\$\begingroup\$

There should be an object that holds your texture and to that object you could add a collider.

\$\endgroup\$
1
\$\begingroup\$

If your game is 2D, you can use Rigidbody2D and BoxCollider2D. It will give you a performance boost, and will ignore Z position differences.

To get OnTriggerEnter or OnTriggerEnter2D, you need to check the "Is Trigger" checkbox in collider settings. Please note that doing so will remove your ability to make rigidbody respond collisions.

\$\endgroup\$
0
\$\begingroup\$

As you have already added "2D collisions", then you can't add "BoxCollider" because it is a 3D object. You have to add the components of the same type. Meaning, if you are going to use the 3D object like "BoxCollider", then you have to add a 3D collision not 2D collision. That means you have to add "Shpare collider" instead of 2D collider.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .