I’m trying to set up a GameObject to function as a sort of button. The idea is that you click on the GameObject and it moves the Main Camera to a different area.
I’ve looked at the OnMouseDown / OnMouseUp methods, but there is a disclaimer saying that it doesn’t work on the iPhone, which is what I’m making this for.
I’ve also tried
void Update (){
if (Input.GetMouseButtonUp(0)){
MoveCamera();
}
}
void MoveCamera(){
Camera.main.transform.position = new Vector3(0, -1024, -500);
}
}
I thought that the script would only occur when you actually clicked on the object, but it happens regardless of where you click.
Unfortunately, making buttons via the GUI is not an option for me at this point. So if you have any ideas on how to make a click GameObject → move Camera script, I’d appreciate the help!
Thanks!
Well, OnMouseDown and OnMouseUp don't work on the iPhone, but that's because iPhones don't have mice! By the same token, anything using Input.GetMouseButtonUp (or Down for that matter) won't work either- try replacing the code with the appropriate Input.GetTouch methods if you are building for iPhone.
Input.GetMouseButtonUp only monitors if the mouse button was released on that frame; it doesn’t care if it was released while on the object or not.
You can use a raycast and check the tag on the returned object (needs to have its collider enabled as well). An example:
if(Input.GetMouseButtonUp(0)) {
RaycastHit hit = new RaycastHit(); // will contain info on what the raycast hit
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // get our ray from camera to mouse position
if(Physics.Raycast(ray, out hit, 100.0f)) { // send a raycast with our ray that is 100.0f units long. Function returns boolean, raycast hit info stored on hit
if(hit.transform.tag == "your_obj_tag") { // hit.transform is the transform of the object that the raycast hit
// do whatever here
}
}
}
Check the Unity docs for more info on each function.
Well, OnMouseDown and OnMouseUp don't work on the iPhone, but that's because iPhones don't have mice! By the same token, anything using Input.GetMouseButtonUp (or Down for that matter) won't work either- try replacing the code with the appropriate Input.GetTouch methods if you are building for iPhone.
– syclamoth