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");
}
}
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:
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 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.
This script gives a message on the console only if the hit gameobject is not in a canvas. When the mouse button is released on a button located inside a canvas, the script doesn't debug anything. How can I fix this?
RaycastHit hit;
void Update ()
{
if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RayHit hit;
if(Physics.Raycast(ray, out hit))
{
// do what you want
Debug.Log(hit.collider.gameObject.tag);
}
}
}
You can use like this to get which UI object is clicked.
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log(EventSystem.current.currentSelectedGameObject.GetComponent<Text>().name);
}
EventSystem.current.currentSelectedGameObject.GetComponent().name
will return clicked object and
EventSystem.current.IsPointerOverGameObject()
will check if an UI object is click or not.
Please keep in mind that I'm new to unity. I created an 2D android game app.
I created a start button( from an image) and attacted a Box colider, and a C# script to it.
When I click the "button" I want the program to move to the next "Level", that Works except that I want it to only Work if i click the object and not everywhere on the game.
This is the C#:
using UnityEngine;
using System.Collections;
public class StartGame : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Application.LoadLevel("Game");
}
}
}
I searched alot about this and maybe people say that to solve this you have to use, Ray and RaycastHit But I cant get that to Work either.
Here is what I tried to with Ray & RaycastHit
// Update is called once per frame
void Update () {
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
RaycastHit hit;
Ray ray;
#if UNITY_EDITOR
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
#endif
if(Physics.Raycast(ray, out hit))
{
Application.LoadLevel("Game");
}
}
}
Any help would be so appriciated.
Probably the easiest way is to add box collider and script component including function below to the gameobject.
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
Application.LoadLevel("Game");
}
}
Edit:
I'm guessing that the original code is not working on your scene, because you are using box collider 2d. Physics.Raycast is testing against 3D colliders. When I run it with the 3D version box collider it works fine.