I have an object hit that contains a Polygon Collider, and in this object I have some objects that contain BoxCollider. Now I'm trying to detect when I click Polygon Collider, and when I click Box Collider. So when I click Box Collider, you should avoid the Polygon Collider.
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
if (hit.collider.GetType() != typeof(BoxCollider2D))
{
Debug.Log("Bad Click");
}
else
Debug.Log("Good Click");
}
So I can not find any way to help me. If anyone has any idea, thank you!!!
This shouldn't work at-all because RaycastHit and Physics.Raycast are used for 3D colliders. For 2D colliders, RaycastHit2D and Physics2D.Raycast should be used. Also, for checking if the object has BoxCollider2D or PolygonCollider2D attached to it, the GetComponent function is used instead of hit.collider.GetType(). It returns null when the component is not available.
Your raycast code should look like something below:
if (Input.GetMouseButtonDown(0))
{
Camera cam = Camera.main;
Vector2 wPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(wPoint, Vector2.zero);
//Check if we hit anything
if (hit)
{
if (hit.collider.GetComponent<BoxCollider2D>() != null)
{
Debug.Log("Bad Click");
}
else if (hit.collider.GetComponent<PolygonCollider2D>() != null)
Debug.Log("Good Click");
}
}
That should fix your problem but I suggest you use the new Event System with OnPointerClick. See #7 from my other answer on how to use it with a 2D collider.
Related
I have a code that allows to position an object onto the other object by Raycast. Obviously, I am using Mesh Collider so everything works fine.
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<SelectableTrunk>())
{
RandomSTrunk.position = hit.point;
}
}
My question is the next. Is it possible to place the object (as I did) and then do one more raycasting to place the other object onto the already placed one?
When I am trying to do that - The mesh collider on the first object is breaking everything and nothing works.
Specify a layer for the hit object and then follow the code below:
public LayerMask hitPlaceLayer;
void RayCast()
{
// first ray cast
if (Physics.Raycast(transform.position, transform.forward, out var hit, hitPlaceLayer.value))
{
RandomSTrunk.position = hit.point;
}
// second raycast
if (Physics.Raycast(transform.position, transform.forward, out hit, hitPlaceLayer.value))
{
otherRandomTrunk.position = hit.point;
}
}
define layer for Hit place object:
Layer mask for raycasts:
playerLayer = 11;
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, crooshair.transform.position - transform.position, 100, ~playerLayer);
if (hit != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
SetFocus(interactable);
}
}
else
Debug.Log("Nothing was hit");
}
Every time my player shoot a raycast it ends up hitting my player. The reason this is is because the raycast is starting inside of the player (which is what I want it to do) but it keeps detecting the player no matter what I do. I've used layers, tried to disable Queries start in colliders, and even starting the raycast from a little bit outside of the player but nothing works. When I try and offset the raycast from the player it sometimes works but that is only as long as you shoot in the direction that the offset is going. Please help.
Does your Player have any childed objects with a Collider that are not layered as playerLayer? Just to assure your layermask is setup correctly, I would advise assigning it in the inspector then invert it using the tilde.
public LayerMask playerLayer;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, crooshair.transform.position, 100, ~playerLayer);
if (hit != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
Debug.Log("Hit" + hit.collider.name);
}
}
}
}
If you are still unable to debug this with assigning the LayerMask in the inspector, if you could post your hierarchy it might help solve the issue.
So I have a script spawning enemies in my scene. Every enemy game object has an enemy script on it that detects a tap or mouse click.
When a click on enemy is detected the health decrements and if it goes below 0 that gameObject is destroyed.
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Enemy")
{
health--;
if (health <= 0)
Destroy(gameObject);
}
}
}
}
The problem is that when I click on an enemy, every enemy in the scene takes damage rather than just the one I am clicking on.
Can't figure out why this is. As for some reason the raycast applies to all the enemies in the scene rather than where I am clicking?
Any ideas?
Thanks
Because you never check which enemy
This line: if (hit.transform.tag == "Enemy") only checks to see if the ray hit an enemy ("oh my god, they shot Bob!"). To check to see if the ray hit itself you want if(hit.transform == this.transform) instead ("I've been shot!").
Or better yet, don't have this code attached to all your enemies (10000 enemies, 10000 raycasts!) and have one script attached to something like the camera that performs the existing check, but replaces health--; line with hit.transform.GetComponent<Enemy>().Damage() which will deal damage only to the enemy the mouse is pointing at.
It's like a mario game. The player is jumping around and has to collect some items.
The problem is that my ray isn't colliding with the item box colliders.
I need the ray to know, so i can destroy the right item that the player has collided.
void OnCollisionEnter2D(Collision2D colisor)
{
if((colisor.gameObject.name == "floor" || colisor.gameObject.name == "floor2" || colisor.gameObject.name == "floor3"))
{
anim.SetBool("jump", false);
anim.SetFloat("speed", 0);
}
if (colisor.gameObject.name == "space(Clone)")
{
RaycastHit hit;
Ray ray = new Ray(player.position, transform.right);
Debug.Log("hit1");
if (Physics.Raycast(ray, out hit))
{
BoxCollider bc2d = hit.collider as BoxCollider;
Debug.Log("hit2");
if (bc2d != null)
{
Destroy(bc2d.gameObject);
}
}
}
}
You're mixing 3d and 2d physics; Physics will only look for 3d objects, so you should be using Physics2D instead. This raycast may still fail if the cast starts inside the target, because the surface normals point in the wrong direction.
Also note that since you already have the Collision2D, you can just grab the otherCollider and shouldn't need to raycast in the first place.
Physics.Raycast isn't working with 2D objects. Instead you need to use Physics2D.Raycast or Graphic Raycaster.
Raycast2D - https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Graphic Raycaster - https://docs.unity3d.com/eng/current/Manual/script-GraphicRaycaster.html
i wanted to show the tooltip like this when the mouse over at the object, here is the example image:
and
i already tried this below code, but the message on the debug.log didn't showed up when i am hovering my mouse to the object, the object i give the name same like this:
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit) && hit.collider.gameObject.name == "Yify")
{
Debug.Log("Yify");
}
}
And here is my object (i use List to multiple the objects and each object i gave the name), (The object's name "Yify" is on the right side, dark green color):
Please help. Thank you.
This should work
void Update(){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit ;
if (Physics.Raycast (ray, hit, 100.0) && hit.collider.gameObject.name=="Yify") {
Debug.Log("Yify");
}
}