indiegamedev

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Create a scene with:

– Cube, attach the script ‘BoomScript.js’


#pragma strict

function Start () {
}

function Update () {
}

function Boom(){
Debug.Log("Kaboooom!");
}

– Main Camera, attach the script ‘Raycast.js’


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something          
          
            // Get Script attached to the GameObject 
            var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);

    		if (boomScript != null) {// if it exists
        	boomScript.Boom(); // execute the function Boom() of BoomScript
   	    	}
        }
    }
 
}// End Update()

Play, click over the Cube, in the console the message will be ‘Kaboooom!’.

For italian people: come funziona?

1. Se viene cliccato il tasto sinistro del mouse

2. parte un raggio dalla Main Camera

3. se colpisce qualcosa ottieni il Component ‘BoomScript’, cioè lo script BoomScript.js

4. se non è nullo, cioè esiste, avvia all’interno di BoomScript.js la funzione Boom()

5. function Boom() manda il messaggio ‘Kaboooom!’ alla console di Unity3D.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag

Once you have determined that your raycast actually hit something, you can query the name of your ‘hit’ object.

Create a scene with:

– Cube, tag it ‘Player’

– Main Camera, attach the script:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something          
           
            var MyObjectTag = hit.collider.tag; // get the tag of the 'hit' object
            
            Debug.Log(MyObjectTag); // the name of the clicked object
            // Do something...
        }
    }
 
}

Play, if you click the Cube you will see inside console ‘Player’.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Physic.Raycast is useful if you need to know the exact point where the mouse pointer is.

Create a scene with:

– Main Camera
– Cube
– Empty Object, assign GetMouseClick.js:


#pragma strict

function Update(){
    // if you press Left Mouse Button -> GetMouseButtonDown(0) 
    if (Input.GetMouseButtonDown(0)){
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something        

            var object = hit.transform.gameObject; // if the ray hits a GameObject
 
            Debug.Log("Clicked!");
            // Do something...
        }
    }
 
}

Play and click over the Cube

NOTICE: You can assign the script at every object in the scene

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject name

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject name

Once you have determined that your raycast actually hit something, you can query the name of your ‘hit’ object.

Create a scene with:

– Cube, remame myCube

– Main Camera, attach the script:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something        
           
            var MyObjectName = hit.collider.gameObject.name; // get the name of the 'hit' object
            
            Debug.Log(MyObjectName); // show the name of the clicked object
            // Do something...
        }
    }
 
}

Play, if you click the Cube you will see inside console ‘myCube’.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject name

Unity3D – OnMouseDown – SetActive – Shuriken Particle System – Javascript

Unity3D – OnMouseDown – SetActive – Shuriken Particle System – Javascript

Unity’s Shuriken Particle System is easy to drive via code!

We will create a simple Asteroid object taht will explode OnMouseDown

Create a scene with:

– Main Camera

– (parent) EmptyObject -> name it Asteroid -> ADD a Box Collider (or OnMouseDown will not work!)
-> Transform Reset

– (child) Mesh -> your mesh -> Transform Reset

– (child) Particle System (MAIN TOP MENU> GameObject> Create Other> Particle System)
-> Inspector> Play On Awake active
-> Transform Reset

Attach to Asteroid (EmptyObject) the script:


#pragma strict

var myParticles : GameObject; // Assign in Inspector

function Start () {
   // Prefab Esplosione, disattivo altrimenti esplode subito alla creazione 
    myParticles.SetActive(false); 
} // END Start

function Update () {	
}// END Update

function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('Particle Activaction!');
        myParticles.SetActive(true); 
}// END OnMouseDown()

Inspector> DRAG AND DROP Particle System over var myParticles

Play and click over the Box Collider of Asteroid (EmptyObject).

For italian peolple: come funziona?

1. Creo un oggetto padre con all’interno la geometria e il sistema particellare
2. Resetto la posizione dei tre game object perchè coincidano
3. Creo un box collider nell’oggetto padre e assegno lo script
4. Lo script ottiene il sistema particellare come GameObject e lo attiva al click. Il sistema particellare parte di sicuro perchè ha attivo ‘Play On Awake’ in Inspector.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – OnMouseDown – SetActive – Shuriken Particle System – Javascript