How To Test Mouse Click Registering
Introduction
This article is about how to notice mouse click or bear upon on a GameObject using C# Script in Unity
Prerequisites
Unity Surroundings version 2018.iv.25f1
Create the project
I pressed ctrl + s to save this scene
Create the Script
Add together a new C# Script, Right-click on Assets. Select Create >> C# script.
Rename the script every bit SceneOneScript.
Create Empty GameObject in Unity
Click on the "GameObject" menu in the card bar. Select "Create Empty" option.
Rename the Create Empty as Scripts
Select the Correct click create new scene
I am going to rename the scene as SceneTwo
Double click to open it create some other script scene - Script again Right-click on Avails. Select Create >> C# script.
Rename the Script every bit SceneTwoScript
Create Empty GameObject in Unity
click on the "GameObject" menu in the menu bar. Select "Create Empty" selection.
Rename the Create Empty equally Scripts
Drag and drop your script to the scripts
Make sure to add together your scenes in the Build Settings window, choose "Build Settings" Add together open scene.
Double click in Scene 1 and it will open Visual Studio 2019, which is my programming integrated evolution environment (IDE) for Unity. Write the lawmaking as shown beneath:
- using System.Collections;
- using Organization.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public course SceneOneScript: MonoBehaviour {
- void Beginning() {}
- void Update() {
- if (Input.GetMouseButtonDown(0)) {
- Ray ray = Camera.master.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- if (hit.transform.name == "Cube" ) {
- SceneManager.LoadScene("SceneTwo" );
- }
- }
- }
- }
- }
Save the Program
Create a cube in Unity.
Click on game object 3d and and so I'm going to create a cube.
Click on SceneTwo open the scene
Create The Sphere in Scene View
Select GameObject, click the 3D object and pick the Sphere option. The Sphere object will exist displayed in the scene view. The name is displayed in the hierarchy view.
InScene Two, double click it and it will open Visual Studio 2019, which is my programming integrated evolution environment (IDE) for Unity. Write the code as shown below:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class SceneTwoScript: MonoBehaviour {
- void Start() {}
- void Update() {
- if (Input.GetMouseButtonDown(0)) {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- if (hit.transform.proper noun == "Sphere" ) {
- SceneManager.LoadScene("SceneOne" );
- }
- }
- }
- }
- }
Save the Program
Play the Application Click the sphere to SceneOne and Click the Cube to SceneTwo every bit Y'all can see is navigating properly so this is the simple way you lot can tap or click on any gameobject
Summary
I hope y'all understood how to find mouse click or touch a GameObject Using C# Script in Unity.
How To Test Mouse Click Registering,
Source: https://www.c-sharpcorner.com/article/how-to-detect-mouse-click-or-touch-on-a-gameobject-using-c-sharp-script-in-unity3d/
Posted by: wilsonbithe1946.blogspot.com
0 Response to "How To Test Mouse Click Registering"
Post a Comment