Moving cubes automatically on the y-axis
I am doing a project like LEGO designer, where you can choose from certain bricks(cubes) and make your own models.
I have managed it to make the cubes move just on the X and Z axis and to stay at the original Y-Position. Now i need to make them go up and down automatically. This means, they normally have to stay on the ground, but when there is a cube, i want to place one cup on the other. I have tried it with creating two empty objects in that cube and to place a Boxcollider in each. So when the upper Collider is in another gameobject, it moves up in Y-direction. But i didn´t manage to get it working.
How can i do that? Or is there a better way to complete my task? Here the code for the movement: public class brickmovement : MonoBehaviour {
public float x;
Vector3
screenPoint,
offset,
scanPos,
curPosition,
curScreenPoint;
float
gridSize = 1f,
yPosOriginal;
void Start () {
//Physics.gravity = new Vector3(0, x, 0); -> Just a test to make the cube fall from the sky
}
void Update () {
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
yPosOriginal = gameObject.transform.position.y;
}
void OnMouseDrag()
{
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.x = (float)(Mathf.Round(curPosition.x) * gridSize +0.5);
curPosition.z = (float)(Mathf.Round(curPosition.z) * gridSize +0.5);
curPosition.y = yPosOriginal;
transform.position = curPosition;
}
}
Here is the code for my upper BoxCollider, which should make the cube move up, if it collides:
Vector3
screenPoint,
offset,
scanPos,
curPosition,
curScreenPoint;
float
gridSize = 1f;
public BoxCollider Box;
private bool touchingup = false;
void Start()
{
}
public void OnCollisionEnter(Collision collision)
{
touchingup = true;
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = (int)(curPosition.y + 1);
curPosition.y = (float)(curPosition.y + 1);
transform.position = curPosition;
}
public void OnCollisionExit(Collision collision)
{
touchingup = false;
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = (int)(curPosition.y + 1);
curPosition.y = (float)(curPosition.y - 0.5);
transform.position = curPosition;
}
}