Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

Need help in Unity C#

A topic by Box Games Studios created Feb 15, 2021 Views: 183 Replies: 9
Viewing posts 1 to 3
Submitted

How can I detect 2 objects colliding with the one that the script is sitting on ? They both must have 2 different tags, thanks!

Submitted(+1)

I am doing a two-person player movement, one moves with the arrow keys, and another moves with WASD. How do I code the movement like that without needing to do get axis raw? Thanks! BTW, this is in the Unity game engine with C#.

Submitted

Well, you could create two more axis which contain the w-s and the a-d keys, and leave the default ones just with the arrow keys.

Submitted(+1)

Oh, thank you!

Submitted

No problem!

Submitted (1 edit) (+2)

In order to detect collision, at least one of the objects need to have a rigid body component. The object with a rigid body will have receive a message (read a method will be called on the attached scripts).

For 3D one these methods will be called depending on if the rigid body hit a collider or a trigger:

  • OnCollisionEnter
  • OnTriggerEnter

For 2D one these methods will be called:

  • OnCollisionEnter2D
  • OnTriggerEnter2D
Submitted(+1)

I know this but I want to detect 2 objects: When 2 object the first with tag "a" and the second with tag "b" touch the object that has the script spawn result. I know how to spawn it.

Submitted (3 edits) (+1)

So you want to detect if both tags are colliding with a specific object?


If that is want you want, you could add a script to that specific object and add a OnCollisionEnter or OnTriggerEnter (2D or 3D, depending on what you want to do), and check for both tags. 

I'd recommend creating two bools: one for a, and one for b, and then setting them on when they collide with the object.

Then in Update() add an if that checks if both bools are on, and then do what you want to do when those 2 objects collide with the specificobject.


Hope this helps!

EDIT: Also, OnTrigger is for colliders with "Is Trigger" on, and OnCollision is for coliders with "Is Trigger" off.

You have Enter, Stay, and Exit which I think are pretty self-explanatory, but just in case:

(the object = object that the script is attached to)

Enter: Used to detect when a object collides with the object.

Stay: Used to detect if an object is still collisioning with the object.

Exit: Used to detect when an object stops colliding with the object.

Submitted(+1)

Thanks, this is what I wanted!

Submitted

No problem!