I’m not good at english. So I explain with picture.
There is a cube(3D).
If I click one side
A cube is created beside mouse clicked side
I worked for a week to make this but I failed. How can I make this?
I’m using java script.
I’m not good at english. So I explain with picture.
There is a cube(3D).
If I click one side
A cube is created beside mouse clicked side
I worked for a week to make this but I failed. How can I make this?
I’m using java script.
Put a cube and a directional light into a scene and then add this script to an empty game object:
#pragma strict
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit) && hit.collider.name == "Cube") {
var v3 = hit.point - hit.transform.position;
var v3Build = Vector3.zero;
if (Vector3.Angle(hit.transform.up, v3) <= 45.0)
v3Build = hit.transform.up;
else if (Vector3.Angle(-hit.transform.up, v3) <= 45.0)
v3Build = -hit.transform.up;
else if (Vector3.Angle(hit.transform.right, v3) <= 45.0)
v3Build = hit.transform.right;
else if (Vector3.Angle(-hit.transform.right, v3) <= 45.0)
v3Build = -hit.transform.right;
else if (Vector3.Angle(hit.transform.forward, v3) <= 45.0)
v3Build = hit.transform.forward;
else if (Vector3.Angle(-hit.transform.forward, v3) <= 45.0)
v3Build = -hit.transform.forward;
if (v3Build != Vector3.zero) {
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = hit.transform.position + v3Build;
go.transform.rotation = hit.transform.rotation;
//go.renderer.material.color = Color(Random.value, Random.value, Random.value);
}
}
}
}
If you are assured that your cubes will be axes aligned, you can simplify the logic by using world coordinates…Vector3.up instead of hit.transform.up for example. You may also want to create your cubes from a prefab rather than CreatePrimitive().