I am trying to make a game object click-able to select it, the game object in question is a building in a top down game,
Here is what is looks like:
Notice how it has a 2d box collider on parent i.e. pfLumberMill and a polygon collider on the child Visual object, The function of the child's collider is for obstacle detection in a grid, here is what I mean :
Now I have a OnMouseDown on building.cs which is attached to the parent pfLumberMill, the purpose of the OnMouseDown is to select that building, i.e. :
private void OnMouseDown() {
// select building
// only triggered where the outer box collider doesn't intersect with the inner polygon collider :(
}
But what is happening is I can select the building only when I click the outer box collider that doesn't collide with the inner polygon collider, I think what happening is the inner collider hit takes precedence over the outer collider hit,
Was thinking of finding the world space on map click and figuring out the selectable area like that, but is there a easier way to this?
2: https://i.stth ack.imgur.com/pOEwk.png
I would put the collider you cant hit in front of the collider you can hit. Then I would use a script to see if you clicked the behind one.
To see how to check if they clicked on anything, see this link
I would only reccomend doing one thing differently.
Assign a variable for your camera, then assign it in start. Then use that variable to screenpointtoray.
Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
......
//Don't do this: Camera.main.ScreenPointToRay(...
//Do this: cam.ScreenPointToRay(...
......
}
Related
I have a game that has a player ship hovering a fixed distance over a tower defense grid. I'd like the tile beneath the ship to highlight when the ship is over it. By using the OnMouseOver() function, this is quite easy, however, I do not want this to be controlled by the mouse but by the player ship. Is there a way to pass mouse event simulations to a game object? How would i go about assigning this function to the ship?
Like derHugo siad you can use OnTriggerStay(). Altough you can also use a raycast to check if something is underneath the ship:
public void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, 10f))
{
//Here obj will be filled with the gameobject under your ship. You can use this to check for the tag, get components and outline the object.
var obj = hit.transform.gameObject;
}
}
Hope this sets you into the right direction.
I am facing a very simple problem about Triggers, but I don't see any way to solve it. I just want the OnTriggerExit function to be called when an object inside the trigger gets disabled.
Here is the way to reproduce the problem in an empty scene :
Create a cube at (0,0,0) with a collider as trigger and the very simple following script :
public class OnTriggerExitTest : MonoBehaviour
{
private void OnTriggerExit( Collider other )
{
Debug.Log( "Exiting : " + other.name );
}
private void OnTriggerEnter( Collider other )
{
Debug.Log( "Entering : " + other.name );
}
}
Create a sphere at (0,0,0) with a Sphere collider and a Rigidbody which does not use Gravity
Move the sphere out of the box, OnTriggerExit gets called
Move the sphere back to (0,0,0), OnTriggerEnter gets called
Disable the sphere, OnTriggerExit is not called
Enable back the sphere, OnTriggerEnter is called
Obviously, I want OnTriggerExit to get called when I disable my sphere. Do you know any solution ?
I am using Unity 5.4.1f
I could use events in the OnDisable function of my sphere of course, but it's not very clean. I simply do not understand why OnTriggerExit is not called but OnTriggerEnter is.
There technically is no reason why Unity could not call OnTriggerExit in your scenario. A likely reason would be however that the "other" Collider is null given that is has been destroyed and would muck up a lot of situations if not handled.
However, you could work around it by using OnTriggerStay instead. It should be called every frame, and the first time it isn't called in your scenario that could mean the other collider got disabled.
I'm making a 2D game, there is one player with two collider : a box collider 2D on top and a small circle collider 2D bottom. When the player jump on the box he will be ok, but when he collide with the box with his face(box collider 2D) he will die ? How can I do that? I tried the following code , but it not work. Please help me.Thanks!
if (GetComponent<Collider2D>().GetType() == typeof(BoxCollider2D))
{
//do something
}
if (GetComponent<Collider2D>().GetType() == typeof(CircleCollider2D))
{
//do something
}
If you want to get a certain BoxCollider, you can use GetComponent<BoxCollider2D>() and GetComponent<CircleCollider2D>()
However, I am not sure how to take this information and check which one fired the the OnCollisionEnter, unless you set one collider to a trigger, and use OnTriggerEnter for one, and OnCollisionEnter for the other.
What I would recommend is having two child game objects on the player and put both colliders on each game object, and handle each event in their own OnCollisonEnter
I have added a particle system where I have checked the collider option and added a world particle collider. See the image below.
In the script that is attached to the particle system I have:
void OnParticleCollision(GameObject other) {
Debug.Log("Particle was hit!");
}
The bullets that are fired donĀ“t seem to hit the particles since the above message is not printed. The bullets are spheres with a sphere collider and a rigidbody attached. The rigidbody is set to non-kinematic (the checkbox is not checked) if that matters.
Also, the bullet object has a script attached with the same lines as above:
void OnParticleCollision(GameObject other) {
Debug.Log("Bullet was hit!");
}
But it is not printed as well.
What am I missing?
Double check that you have this script attached to your particle system and not any arbitrary gameobject.
Check if you have "Is trigger" disabled on Sphere Collider, or you're particles too small and don't hit the actual collider.
Tried to simulate your situation, all works fine.
I am making one unity game in which two objects having collider in which I have select isTriger and does not have rigid body, if I put the rigid body then they are kinematic object, So that gravity did not effect on that object, Even also i don't want any physic operation on this object also. but I want to detect whether this two object collide which each other or not.
How can I do this ?
When 2 colliders make contact with each other,
OnCollisionEnter2D
OnCollisionExit2D
OnCollisionStay2D
are called for 2D games, likewise for 3D(remove 2D in names) also.
Check out this link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
Sorry man, but unity use the phsysics engine to detect collision so you have to add the rigidbody to the item you want to to plug the script.
PS:remeber if you want to detect collisions with Trigger collider, you need to use
void OnTriggerEnter(){
//your code
}
void OnTriggerStay(){
//your code
}
void OnTriggerLeave(){
//your code
}