How do I change the block? Unity 3D c #

0

I have the following code:



using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class BuildSystem : MonoBehaviour { RaycastHit hit; Ray ray; Vector3 Paredposition;

public int elevacion;
public Transform CuboObjeto, Cubo, Cubo2;
public LayerMask layer;

public void Boton1(){
coso ();
        if (Input.GetMouseButtonDown (0)) {
            Instantiate (Cubo, CuboObjeto.position, CuboObjeto.rotation);

    }
}
public void Boton2(){
    coso ();
    if (Input.GetMouseButtonDown (0)) {
    Instantiate (Cubo2, CuboObjeto.position, CuboObjeto.rotation);
}
}
    void coso(){
    ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if(Physics.Raycast(ray, out hit, 5, layer)){
        Paredposition = hit.point;
            Paredposition.y = 0f;
        CuboObjeto.position = Paredposition;
    }
    }

}

What I try to do there is that by pressing a button change to what corresponds

I have 2 buttons, one to appear black and the other blue

So when I press for example, black, that I can just put that block and if then I play blue, I can just put the blue, etc.

    
asked by Alien69 16.12.2017 в 05:54
source

1 answer

0

If what you want to do is select the cube that you are going to instantiate when you press a button and position it in the world when you click, you can use something like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BuildSystem : MonoBehaviour {
RaycastHit hit;
Ray ray;
Vector3 Paredposition;

public int elevacion;
public Transform CuboObjeto, Cubo, Cubo2;
public LayerMask layer;

private Transform currentCube;

void Update()
{
    coso();
    if (Input.GetMouseButtonDown (0)) {
        Instantiate (currentCube, CuboObjeto.position, CuboObjeto.rotation);
    }
}

public void Boton1(){
    currentCube = Cubo;
}
public void Boton2(){
    currentCube = Cubo2;
}
void coso()
{
    ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if(Physics.Raycast(ray, out hit, 5, layer)){
        Paredposition = hit.point;
        Paredposition.y = 0f;
        CuboObjeto.position = Paredposition;
    }
}
}

I hope it helped you

    
answered by 05.01.2018 / 17:48
source