banner



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:

  1. using  System.Collections;
  2. using  Organization.Collections.Generic;
  3. using  UnityEngine;
  4. using  UnityEngine.SceneManagement;
  5. public course  SceneOneScript: MonoBehaviour {
  6. void  Beginning() {}
  7. void  Update() {
  8. if  (Input.GetMouseButtonDown(0)) {
  9.       Ray ray = Camera.master.ScreenPointToRay(Input.mousePosition);
  10.       RaycastHit hit;
  11. if  (Physics.Raycast(ray, out  hit)) {
  12. if  (hit.transform.name == "Cube" ) {
  13.           SceneManager.LoadScene("SceneTwo" );
  14.         }
  15.       }
  16.     }
  17.   }
  18. }

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:

  1. using  System.Collections;
  2. using  System.Collections.Generic;
  3. using  UnityEngine;
  4. using  UnityEngine.SceneManagement;
  5. public class  SceneTwoScript: MonoBehaviour {
  6. void  Start() {}
  7. void  Update() {
  8. if  (Input.GetMouseButtonDown(0)) {
  9.       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  10.       RaycastHit hit;
  11. if  (Physics.Raycast(ray, out  hit)) {
  12. if  (hit.transform.proper noun == "Sphere" ) {
  13.           SceneManager.LoadScene("SceneOne" );
  14.         }
  15.       }
  16.     }
  17.   }
  18. }

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel