Collider.isTouching for 3D colliders
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) {
score++;
this.GetComponent<Renderer> ().material.color = col.GetComponent<Renderer> ().material.color;
}
if (spriteBox.IsTouching(col) && col.CompareTag("barrier") ) {
Debug.Log (col);
Debug.Log (colliderBox);
SceneManager.LoadScene (mainMenu);
}
Currently in my code, I was using a onTriggerEnter, to detect a collision with an object called 'barrier', but I have had to replace my 2d spriteBox and 2d colliderBox with 3D objects, and now isTouching is not a property of 3D colliders. I can not find a simple replacement. What can I replace them with?
c# unity3d 3d
add a comment |
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) {
score++;
this.GetComponent<Renderer> ().material.color = col.GetComponent<Renderer> ().material.color;
}
if (spriteBox.IsTouching(col) && col.CompareTag("barrier") ) {
Debug.Log (col);
Debug.Log (colliderBox);
SceneManager.LoadScene (mainMenu);
}
Currently in my code, I was using a onTriggerEnter, to detect a collision with an object called 'barrier', but I have had to replace my 2d spriteBox and 2d colliderBox with 3D objects, and now isTouching is not a property of 3D colliders. I can not find a simple replacement. What can I replace them with?
c# unity3d 3d
add a comment |
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) {
score++;
this.GetComponent<Renderer> ().material.color = col.GetComponent<Renderer> ().material.color;
}
if (spriteBox.IsTouching(col) && col.CompareTag("barrier") ) {
Debug.Log (col);
Debug.Log (colliderBox);
SceneManager.LoadScene (mainMenu);
}
Currently in my code, I was using a onTriggerEnter, to detect a collision with an object called 'barrier', but I have had to replace my 2d spriteBox and 2d colliderBox with 3D objects, and now isTouching is not a property of 3D colliders. I can not find a simple replacement. What can I replace them with?
c# unity3d 3d
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) {
score++;
this.GetComponent<Renderer> ().material.color = col.GetComponent<Renderer> ().material.color;
}
if (spriteBox.IsTouching(col) && col.CompareTag("barrier") ) {
Debug.Log (col);
Debug.Log (colliderBox);
SceneManager.LoadScene (mainMenu);
}
Currently in my code, I was using a onTriggerEnter, to detect a collision with an object called 'barrier', but I have had to replace my 2d spriteBox and 2d colliderBox with 3D objects, and now isTouching is not a property of 3D colliders. I can not find a simple replacement. What can I replace them with?
c# unity3d 3d
c# unity3d 3d
edited Nov 23 '18 at 21:42
Programmer
77.2k1089158
77.2k1089158
asked Nov 23 '18 at 21:15
MattattackMattattack
778
778
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There is no equivalent version of the isTouching
function for 3D collision but you can roll your own. When OnTriggerEnter
, store both colliders involved in a collection. When OnTriggerExit
is called, check for both colliders involved and if already exit in the collection, remove them from the collection. This is what I am doing in this post with the CollisionDetection
script.
Attach the CollisionDetection
script to the objects that will be colliding.
To check if Collider
is touching another Collider
, change:
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) { }
to:
if (CollisionDetection.IsTouching (colliderBox.gameObject, col.gameObject) && col.CompareTag("barrier")) { }
You can also add ExtensionMethod for the CollisionDetection class:
public static class ExtensionMethod
{
public static bool IsTouching(this Collider collider, Collider otherCollider)
{
return CollisionDetection.IsTouching(collider.gameObject, otherCollider.gameObject);
}
public static bool IsTouching(this GameObject collider, GameObject otherCollider)
{
return CollisionDetection.IsTouching(collider, otherCollider);
}
}
Now, your if (colliderBox.isTouching (col) && col.CompareTag("barrier"))
code should work without changes assuming that colliderBox
is a type of Collider
or GameObject
.
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453014%2fcollider-istouching-for-3d-colliders%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no equivalent version of the isTouching
function for 3D collision but you can roll your own. When OnTriggerEnter
, store both colliders involved in a collection. When OnTriggerExit
is called, check for both colliders involved and if already exit in the collection, remove them from the collection. This is what I am doing in this post with the CollisionDetection
script.
Attach the CollisionDetection
script to the objects that will be colliding.
To check if Collider
is touching another Collider
, change:
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) { }
to:
if (CollisionDetection.IsTouching (colliderBox.gameObject, col.gameObject) && col.CompareTag("barrier")) { }
You can also add ExtensionMethod for the CollisionDetection class:
public static class ExtensionMethod
{
public static bool IsTouching(this Collider collider, Collider otherCollider)
{
return CollisionDetection.IsTouching(collider.gameObject, otherCollider.gameObject);
}
public static bool IsTouching(this GameObject collider, GameObject otherCollider)
{
return CollisionDetection.IsTouching(collider, otherCollider);
}
}
Now, your if (colliderBox.isTouching (col) && col.CompareTag("barrier"))
code should work without changes assuming that colliderBox
is a type of Collider
or GameObject
.
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
|
show 1 more comment
There is no equivalent version of the isTouching
function for 3D collision but you can roll your own. When OnTriggerEnter
, store both colliders involved in a collection. When OnTriggerExit
is called, check for both colliders involved and if already exit in the collection, remove them from the collection. This is what I am doing in this post with the CollisionDetection
script.
Attach the CollisionDetection
script to the objects that will be colliding.
To check if Collider
is touching another Collider
, change:
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) { }
to:
if (CollisionDetection.IsTouching (colliderBox.gameObject, col.gameObject) && col.CompareTag("barrier")) { }
You can also add ExtensionMethod for the CollisionDetection class:
public static class ExtensionMethod
{
public static bool IsTouching(this Collider collider, Collider otherCollider)
{
return CollisionDetection.IsTouching(collider.gameObject, otherCollider.gameObject);
}
public static bool IsTouching(this GameObject collider, GameObject otherCollider)
{
return CollisionDetection.IsTouching(collider, otherCollider);
}
}
Now, your if (colliderBox.isTouching (col) && col.CompareTag("barrier"))
code should work without changes assuming that colliderBox
is a type of Collider
or GameObject
.
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
|
show 1 more comment
There is no equivalent version of the isTouching
function for 3D collision but you can roll your own. When OnTriggerEnter
, store both colliders involved in a collection. When OnTriggerExit
is called, check for both colliders involved and if already exit in the collection, remove them from the collection. This is what I am doing in this post with the CollisionDetection
script.
Attach the CollisionDetection
script to the objects that will be colliding.
To check if Collider
is touching another Collider
, change:
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) { }
to:
if (CollisionDetection.IsTouching (colliderBox.gameObject, col.gameObject) && col.CompareTag("barrier")) { }
You can also add ExtensionMethod for the CollisionDetection class:
public static class ExtensionMethod
{
public static bool IsTouching(this Collider collider, Collider otherCollider)
{
return CollisionDetection.IsTouching(collider.gameObject, otherCollider.gameObject);
}
public static bool IsTouching(this GameObject collider, GameObject otherCollider)
{
return CollisionDetection.IsTouching(collider, otherCollider);
}
}
Now, your if (colliderBox.isTouching (col) && col.CompareTag("barrier"))
code should work without changes assuming that colliderBox
is a type of Collider
or GameObject
.
There is no equivalent version of the isTouching
function for 3D collision but you can roll your own. When OnTriggerEnter
, store both colliders involved in a collection. When OnTriggerExit
is called, check for both colliders involved and if already exit in the collection, remove them from the collection. This is what I am doing in this post with the CollisionDetection
script.
Attach the CollisionDetection
script to the objects that will be colliding.
To check if Collider
is touching another Collider
, change:
if (colliderBox.isTouching (col) && col.CompareTag("barrier")) { }
to:
if (CollisionDetection.IsTouching (colliderBox.gameObject, col.gameObject) && col.CompareTag("barrier")) { }
You can also add ExtensionMethod for the CollisionDetection class:
public static class ExtensionMethod
{
public static bool IsTouching(this Collider collider, Collider otherCollider)
{
return CollisionDetection.IsTouching(collider.gameObject, otherCollider.gameObject);
}
public static bool IsTouching(this GameObject collider, GameObject otherCollider)
{
return CollisionDetection.IsTouching(collider, otherCollider);
}
}
Now, your if (colliderBox.isTouching (col) && col.CompareTag("barrier"))
code should work without changes assuming that colliderBox
is a type of Collider
or GameObject
.
answered Nov 23 '18 at 22:02
ProgrammerProgrammer
77.2k1089158
77.2k1089158
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
|
show 1 more comment
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
I'm going to have a go with this, thanks! Is it recommended to use 3D colliders on 2D games? I have found some people saying to always use 3D colliders, however for my game it is easier to use 2D ones, but it seems to prohibit me from rotating the way I want (with rigidbody.rotation) to and access all the rigidbody features? Any advice?
– Mattattack
Nov 24 '18 at 10:50
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
No. Use 2D colliders with Sprites/SpriteRenderer. Use 3D colliders for models/MeshRenders. The-something applies to Rigidbody. It must match. I really can't suggest anything more than this until I see the type of game you're making
– Programmer
Nov 24 '18 at 10:52
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
Would you mind looking here for more reference? It explains my original problem. Thank you
– Mattattack
Nov 24 '18 at 11:04
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
That doesn't help. Is your character images or 3d model? A screenshot might also help
– Programmer
Nov 24 '18 at 11:09
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
The image is 2D, it is an endless game where you simple move left and right to avoid objects.
– Mattattack
Nov 24 '18 at 11:29
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453014%2fcollider-istouching-for-3d-colliders%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown