- Home /
3d object like PLANE act as clickable button?
May someone teach or guide me how to make a 3d object like plane as an clickable button that redirect me to the other scene?
Answer by jacksonbarnes · Aug 13, 2018 at 09:26 PM
Hi here is a simple script to make an object act like a button with a OnClick event. Just attach this script to a 3d object you would like to make a button. Cheers
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class WebsiteButton : MonoBehaviour {
public GameObject definedButton;
public UnityEvent OnClick = new UnityEvent();
// Use this for initialization
void Start () {
definedButton = this.gameObject;
}
// Update is called once per frame
void Update () {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit Hit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out Hit) && Hit.collider.gameObject == gameObject)
{
Debug.Log("Button Clicked");
OnClick.Invoke();
}
}
}
}
Answer by Suley · Mar 14, 2017 at 09:36 AM
Hi,
you can find a few Posts about this:
You have two Options. One is to use OnMouseDown or you could use Raycast to "hit" the 3d Object, but dont forget to put a collider on the 3d Object. You can see further details on those two links, which are provided.
I'm pretty sure you can, specially On$$anonymous$$oueDown ().
Actually, i think unity's On$$anonymous$$ouseDown funcion casts a ray to hit the object so there is little reason to create the ray. Unless you want more funcionalilty...
Your answer
Follow this Question
Related Questions
Objects can't be seen in scene view and appear again (In editor mode, not running) 0 Answers
Is it possible to get terrain tools on imported terrrain ? 1 Answer
How to add touchscreen controls to this script? 0 Answers
(CLOSED) Plane Failing to Render When Camera is Switched Until Scene is Reloaded?! 0 Answers
Can't Change Scenes When Built 4 Answers