Detecting if two game objects are overlapping / intersecting Unity - c#

I have what I think is a straight forward question, but my lack of unity knowledge is hindering me.
I have primitive type game objects (e.g. a cube / capsule / sphere) rotated at various angles in my scene. They are already instantiated.
I then add a new game object primitive type to the scene and I want to know if this new game object overlaps / intersects the current primitive types in the scene.
To do this I have been using the gameobject1.GetComponent<Collider>.bounds.Intersects(gameobject2.GetComponent<Collider>.bounds)
However this doesn't work because the bounds is a bounding box so even if a game object isn't directly touching one of the primitive type game objects visually, if it's in it's bounding box, it returns that as true (i.e. they intersect) which is not what I want:
I'm sure there is another way of doing this in unity but I just don't know what I need to do!
Any help would be appreciated. Thanks

Use MeshCollider, instead of BoxCollider
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}

You could use a MeshCollider instead (as long as your objects are simple) then set the MeshCollider IsTrigger property to true.
Then in code you can do:
void OnTriggerEnter(Collider col)
{
}
or
void OnCollisionEnter(Collision col)
{
}
When ever an object enters the collider, the above function will be called and you can do what you need to there.
Edit: Your objects will need a rigidbody added to them for the collision to work

Related

Is there any way to get an object that can push/move a character controller?

I know you can use rigid body on two objects for collision and so on. I can do that just fine but I’m trying to do this without needing one on my player object.
But if I have a player object that is using only a character controller, is there any way to “push” them around when colliding with other objects?
As the CharacterController inherits from Collider you could do this if the other objects have a (kinematic) Rigidbody - at least one Rigidbody is required so the collision itself is detected (not sure on this though, might work without, the CharacterController is a special case) - and then use Physics.ComputePenetration in order to calculate the distance and direction you have to move your player in order to move it out of the hit collider => pushing it away.
[RequireComponent(typeof(Rigidbody))]
public class PushCharacter : MonoBehaviour
{
[SerializeField] Collider ownCollider;
void Awake ()
{
if(! ownCollider) ownCollider = GetComponentInChildren<Collider>();
}
void OnCollisionStay(Collision collision)
{
if(collision.collider is CharacterController character)
{
Physics.ComputePenetration(ownCollider, ownCollider.transform.position, ownCollider.transform.rotation, character, character.transform.position, character.transform.rotation, out var direction, out var distance);
character.Move(direction * distance);
}
}
}
Note also
One of the colliders has to be BoxCollider, SphereCollider CapsuleCollider or a convex MeshCollider. The other one can be any type.
Which limits the objects you can use as the pushing objects since the CharacterController is none of those (even though it kinda acts like a Capsule).
However - just as a heads-up - as soon as there are multiple pushing objects involved this could get quite complex!
Yes, of course it is possible to do it without using built in colliders and rigid bodies. They are just script after all.
One way to do this, is to see if one of the outer bounds of the player object are within Vector3-radius and Vector3+radius then apply force to the RigidBody or move the players Vector3.
There are countless possibilities for how to do this but the easiest way is definitely using colliders and RigidBodies..

Child Objects Not Following Parent Object On Frequent Teleportation

Game Screen
Teleportation Code
Child Objects And Their Original Location
I'm new to Unity, and so after taking just some regular online courses for Unity2D I wanted to mess around with adding in different features, the first of which I decided to do was something like in Portal, where a projectile spawns two connected portals you can teleport between. However, I've run into an issue. When I'm teleporting my character, sometimes, usually when I'm teleporting too quickly but can happen at any time, the child objects to the Player game object tend to shift, and I don't understand why. **I'd like to:
Know why the offset between the parent and child objects are changing through teleportation.
Know how to fix this issue, preferably in code I can easily understand as a beginner to Unity. Also preferably in a way that doesn't involve me constantly appending the child objects to the parent object through transform position with the added offset, though if it's the simplest solution I'm not against trying it.**
Something worth noting is that the offset change is different as well, I have a Child Object called Feet which detect the Ground for jumping, which seems to remain at the location of the previous portal when it first breaks. However, another child object called Gun which is where the projectiles spawn from seem to only move down a little bit, meaning there's inconsistency in how they are offset when they break. It might be because the Feet has a collider, but I'm unsure, don't know enough, and only felt it was worth mentioning.
[SerializeField] GameObject otherPortal;
Portal otherPortalComponent;
BoxCollider2D boxCollider2D;
bool firstEntered = true;
// Start is called before the first frame update
void Start()
{
boxCollider2D = GetComponent<BoxCollider2D>();
otherPortalComponent = otherPortal.GetComponent<Portal>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Player")) && firstEntered)
{
Teleport(collision.gameObject);
otherPortalComponent.SetFirstEnteredFalse();
}
}
private void Teleport(GameObject obj)
{
obj.transform.position = otherPortal.transform.GetChild(0).transform.position;
}
public void SetFirstEnteredFalse()
{
this.firstEntered = false;
}
private void OnTriggerExit2D(Collider2D collision)
{
this.firstEntered = true;
}
To simplify the question, the position of the child objects relative to the parent changes when I instantly change the parents position sometimes, why does this happen and how do I fix the issue without simply using transform.position in an Update method to constantly append the child to the parent, if possible.
I would look at your OnTriggerEnter2D method's if statement for a solution.
private void OnTriggerEnter2D(Collider2D collision)
{
if (boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Player")) && firstEntered)
{
Teleport(collision.gameObject);
otherPortalComponent.SetFirstEnteredFalse();
}
}
Because you are using OnTriggerEnter this method will only be called with your feet since your player's main collider isn't a Trigger. This means that your collision variable that you call your Teleport method on is actually your feet object, not your player's body object. So you are changing the offset of your feet from your player in your Teleport method.
I would try changing your collision method to OnCollisionEnter2D(Collision2D)
which would pick up your player's base collider when it enters and not the feet collider.
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.GetComponent<Player>() && firstEntered)
{
Teleport(collision.gameObject);
otherPortalComponent.SetFirstEnteredFalse();
}
}
Since the Player script is on the same object as your base player collider you can do a simple GetComponent<Player>() call to check if its the player object. Though you could still use the Layer to check if you want.

Get gameobject of unity collider when it is attached to a rigidbody

I have a game object that has a rigidbody and then a group of sub game objects with sprites and colliders where each collider is attached to the parent's rigidbody.
This works well for the physics and collisions as the entire group of objects will bounce and collide off of the scenery.
However, when two groups collide I want their to be damage on one of the individual sub game objects.
I have a OnCollisionEnter2D(Collision2D coll) on each of the sub objects(Which have the collider on them) however, when they collide with another group using coll.gameObject the returned game object is always the parent and not the individual sub object.
Long Story Short:
Is there any way to get the game object of a collider when it is attached to another game object with a rigid body?
Note: I have seen some solutions that use a ray cast to find the object but it seems like a lot of unnecessary work.
Note 2: I have also seen options that use trigger but i prefer the collision as it handles physics as well.
private void OnCollisionEnter2D(Collision2D coll)
{
Debug.Log(coll.gameObject.name); // Showing the parent
ShipPiece sp = coll.gameObject.GetComponent<Piece>(); // Looking for the individual piece
if (sp != null)
{
// Apply the damage to the other piece based off the weight of this piece
coll.gameObject.SendMessage("ApplyDamage", weight*10);
}
}
Obviously I can can the first Piece in the collision as it is the class where OnCollisionEnter2D exists, but I cannot figure out a way to get the second Piece which its colliding into.
You're trying to get the gameObject property of the Collision2D object when you really want the gameObject properties of the Collider2D itself. Collision2D has the collider and otherCollider properties that you can use:
private void OnCollisionEnter2D(Collision2D coll)
{
Debug.Log(coll.collider.gameObject.name);
Debug.Log(coll.otherCollider.gameObject.name);
...
}

how to change object's B color when object A is in range? 2D

I have an enemy AI object that randomly patrols a top down 2d maze(object B) and within this maze are tiles(multiple objects of A). I'm having trouble figuring out a way to change the color of only 1 tile that is within close distance to the enemy object B. Here is my code:
public Transform blocks;
private void Update()
{
blocks = GameObject.FindGameObjectWithTag("Breakable_Block").transform;
if (Vector2.Distance(transform.position, blocks.position) < 10)
{
blocks.GetComponent<Renderer>().material.color = Color.green;
}
}
What I'm trying to do with my project is to have an invisible enemy object patrol randomly in the maze and when it comes in contact or range of 1 tile the tile changes color. When the enemy moves away the tile changes back to it's regular color.The reason why I'm using distance instead of collision is because I cannot have the enemy object crash and get stuck with the tiles themselves.
Thanks in advance!
EDIT
I mistakenly read FindGameObjectWithTag as FindGameObjectsWithTag. The first returns a single GameObject, the second returns an array of GameObjects. So the next paragraph is incorrect. If there are going to be multiple "Breakable_Block" objects I'd still lean towards using Triggers. If there is only ever a single "Breakable_Block", then a Vector2.Distance could be the more appropriate.
So, a quick note, GameObject.FindGameObjectWithTag("Breakable_Block") will return an array of GameObjects, so the code example you've given won't be able to find the distance from your player to the whole array. You'd need to find the distance to the indivual objects. Which involves more code. This is the reason I think using the tools available in Unity might be simply easier.
So, I'd implement it this way:
Add a CircleCollider2D to your enemy (Object B). Mark it as a
Trigger.
Add a BoxCollider2D to your blocks ( assuming they're
rectangular ).
Implement OnTriggerEnter2D, and OnTriggerExit2D to change
your material properties.
Here's is a quick code example you could place on your Enemy (Object B).
public class Enemy : MonoBehaviour
{
void OnTriggerEnter2D ( Collider2D other )
{
other.GetComponent<Renderer>().material.color = Color.green;
}
void OnTriggerExit2D ( Collider2D other )
{
other.GetComponent<Renderer>().material.color = Color.red;
}
}

Find and add the velocity of a Rigidbody that's directly below another? (Unity + C#)

I have an issue that I've not been able to solve for a few days now and without it working I can't move on with my project, so your help would be greatly appreciated!
What I'm trying to do is 'find' the velocity of an object that is directly below my 2D sprite (that also contains a Rigidbody and 2D box collider) and then add that velocity (in the same direction) to the object that is 'looking' for it.
I feel like ray-casting might be part of the answer but I'm not sure how to use that, especially in this context.
The idea behind this is I have a platform that can carry objects stacked on top of each other, so you move the mouse, it manipulates the platforms velocity, but this causes the objects on-top to fly backwards, no mater how high the friction is.
Here is the platform code:
void Update()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
mouseDelta = pos_move - lastMousePosition;
acceleration = mouseDelta.x * 0.4f;
platformVelocity += acceleration;
platform.velocity = new Vector2(platformVelocity, 0) * speed;
lastMousePosition = pos_move;
}
Thank you for taking the time to read this, I would very much appreciate any help you can give!
Is it necessary to 'find' that object? 'Finding' may be difficult & costing.
Can you hold the 'below-thing' reference and pass it to 'above-thing'?
I'm assuming you have some method on objects that checks whether it's on a moving platform. One option is to add an IsOnMovingPlatform flag on those objects. Then use a similar method to check if any object is on top of the "IsOnMovingPlatform == true" objects. Keep doing passes on that method until you don't come up with any more flags set to true.
So, i'm trying to understand your question and I think I might know what you're talking about.
Are you saying that you want your game object (2d sprite) to stay on the platform as its moving? I have had the same problem, where a platform is moving and my player (game object) slides off- and this is how I fixed it.
Add a box collider to the platform (trigger). When the player enters the platform, make the player's transform the platform. That way, when the platform moves- the player moves with it no matter what the speed is.
For example:
void OnTriggerEnter2D(Collider2d collider){
if(collider.tag.equals("Player")){ // you only want the player to work
// attach script to platform for "this.transform" to work
collider.transform.SetParent(this.transform);
}
}
then, to unparent the transform "otherwise, even when the player is off the platform, it will move with the platform.
void OnTriggerExit(Collider2d collider){
// assuming your player isn't supposed to have a parent!
collider.transform.parent = null;
}
Sorry if I misunderstood your question! If this is what you were going for, finding the velocity of the platform is not necessary. Also, this is uncompiled code- if you have issues let me know.
EDIT:
To add a bit of a give, for the entire duration that the player is in contact with the platform, we can add a little bit of a negative force to shove the player a little bit!
We do this by using OnTriggerStay2D (assuming your game is 2d, otherwise use OnTriggerStay).
For example:
void OnTriggerStay2D(Collider2d collider){
if(collider.tag.equals("Player")){
collider.getComponent<Rigidbody2D>().addForce(new vector2(forceToAdd, 0));
}
}
now, that "forceToAdd" variable should be based on the speed and direction that your platform is going. Does your platform have a rigid body on it? if so you can get your force variable by retrieving the rigid body from the platform (getcomponent()) and get the X velocity from it. With this velocity, experiment with dividing it by numbers until you find a good fit, multiply it by -1 (because you want to push the player in the opposite direction of the platform travel), then this will be your forceToAdd!
However, if your platform is not using a Rigidbody, then a way you can do this is record the x positions in your script (just two positions, and subtract these to find the distance in an update method), multiply by -1, try dividing by a number (experiment with this) and that can be your force to add variable.
Also, my code for the OnTriggerStay2d is rough, the spelling could be a bit wrong- but this will give you a rough idea for a solution! ALSO.. I don't think getting the component of a rigid body should be done in every frame, a way around this would to have your rigid body component as a global variable in the script, then set your rigid body on OnTriggerEnter2d, this way it will only retrieve it once (instead of every frame) which I assume would be more efficient!
Good luck! and if you have any questions il try to help!
This is a raycast2d version, I tested it and it works much better than the trigger collider version :).
using UnityEngine;
using System.Collections;
public class RaycastFeetDeleteLater : MonoBehaviour {
[SerializeField] private float raycastDist = 0.1f;
[SerializeField] Transform feet;
[SerializeField] private float xForceDampener = 0.1f;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate () {
RaycastDown ();
}
private void RaycastDown(){
RaycastHit2D hit = Physics2D.Raycast (feet.position, Vector2.down, raycastDist);
if (hit.collider != null) {
print (hit.transform.tag);
if (hit.collider.tag.Equals ("Platform")) {
transform.SetParent (hit.transform);
Vector2 force = new Vector2 ((hit.transform.gameObject.GetComponent<Rigidbody2D> ().velocity.x * xForceDampener) * -1, 0);
print (force);
rb2d.AddForce (force);
} else if (transform.parent != null) {
print ("UNPARENT");
transform.parent = null;
}
} else {
if (transform.parent != null) {
transform.parent = null;
}
}
Debug.DrawRay (feet.position, Vector3.down, Color.green, raycastDist);
}
}
This script is attached to the player! No need to have scripts on the platforms, which was the main reason my previous method was a pain :P
Basically this script does the following:
- Gets rigidbody2d of the player on start (we will be adding forces to the player with it soon, so might as well store it in a variable!
On FixedUpdate (Different than Update, FixedUpdate is called on Physics update, where as Update is called every frame) I have read that raycast should use FixedUpdate, I suppose because it is a Physics method!
RaycastDown shoots a 2D raycast directly down from the feet position (to get the feet, create a child object of the player, put the position of the feet a tiny bit below the player, NOT ON THE PLAYER- I will explain why in a second why that will mess up everything! Then drag the feet GameObject on to the "feet" slot of the script.)
Raycast shoots from feet directly down at a distance that is changeable in the editor (raycastDist) I found a distance of 0.1f was perfect for my needs, but play around with it for yours! You can actually see a drawing of your Ray by using Debug.DrawRay as seen in the code, the ray is only visible in the editor- not game view! But this will help, especially with positioning the feet.
Checks if it hit an object with a collider, if so check if the objects tag is a Platform (set the platforms to a new tag and call it Platform).
If it hits a platform set the parent of the player to the platform (like the last script) and start applying a little force to the player in the negative direction that the platform is traveling (we get the rigid body of the platform to find its velocity).
If the raycast does not hit a platform, unparent it so the player can move freely.
Thats basically it, however...
Please note:
This script needs to be on the player, not any of the players child objects, since this script will unparent the player from any platforms- if you have the script on a child object of the player it will unparent from the player- which is horrible. So The script MUST be on the root game object of the player. There are ways around this if for some reason you can't.
As mentioned before, the "feet" child object must be a little bit under the player- this is because when the Raycast2d "fires" it will find the FIRST object with a collider it hits- so if you have the start of the raycast hit a collider on the player then it will always show the raycast hitting the player, not a platform. So to get around this have the raycast fire from the feet- and have the feet position a tiny bit below the player. To help with this I added a print statement that will print to the console which object the raycast is hitting, if it doesn't say "Platform" then you need to lower the feet! :)
Furthermore, I'm not sure how you are moving your platforms- but if the Velocity of the platform always prints "0,0" then its because you are moving the platforms position directly, instead of moving it by forces (this happened to me when I was testing this script). I got around this by moving the platform by "AddForce". Also depending on the "Mass" of your platforms, you may need to drastically increase the "xForceDampener" which I should of actually labeled "xForceMultiplier" as I found while testing I needed to set it much higher- like at 60, instead of 0.25 which I previously thought would be okay..
Hope this helped :D
EDIT: Linking the unity docs for 2D Raycasts in case you need
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

Categories