Hi everyone,
I’m using this script to create walls at runtime:
using UnityEngine;
using System.Collections;
public class CreateWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
public GameObject wallPrefab;
GameObject wall;
void Start()
{
}
void Update()
{
GetInput();
}
void GetInput()
{
if(Input.GetMouseButtonDown(0))
{
setStart();
}
else if(Input.GetMouseButtonUp(0))
{
setEnd();
}
else{
if(creating)
{
adjust();
}
}
}
void setStart()
{
creating = true;
start.transform.position = getWorldPoint();
wall = (GameObject)Instantiate(wallPrefab, start.transform.position, Quaternion.Identity);
}
void setEnd()
{
creating = false;
end.transform.position = getWorldPoint();
}
void adjustWall()
{
start.transform.LookAt(end.transform.position);
end.transform.LookAt(start.transform.position);
float distance = Vector3.Distance(start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distance/2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3(wall.transform.localScale.x,wall.transform.localScale.y, distance);
}
void adjust()
{
end.transform.position = getWorldPoint();
adjustWall();
}
Vector3 getWorldPoint()
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit it;
if(Physics.Raycast(ray, out hit))
{
return hit.point;
}
return Vector3.zero;
}
It creates a wall in the middle of two poles and after resize it on one axis according to the distance of the two poles.
But once the walls have been built, how can I resize them by giving the width of a wall as input?
I succeded in resizing the first wall, but since the room can have any size and any number of wall (walls always have a snap of 90°) look really complicated to do this.
Did anyone already made something like this?
Thank you.