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.
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:
Im making a system where you can press a certain key and it will place down an object at your mouse location. Every time you press the key '1' it should place it down at the mouse location but for some reason every frame the Input.GetKeyDown("Alpha1"); is registered as true so wherever i move my mouse it places the block down no matter what I press. This has been happening to me alot recently and I cant seem to find any answers.
using UnityEngine;
public class CubePlacer : MonoBehaviour
{
private Grid grid;
public KeyCode place;
private void Awake()
{
grid = FindObjectOfType<Grid>();
}
private void Update()
{
if (Input.GetKeyDown(place)) ;
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
}
private void PlaceCubeNear(Vector3 clickPoint)
{
var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;
//GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = nearPoint;
}
}
if (Input.GetKeyDown(place)) ; // <--- remove this semi colon
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
You need to remove the semicolon after the If statement. It terminates the line and causes the block afterwards to execute every time the Update() method is called.
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.
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.
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");
}
}