:)Hey Unity team,
I have been working on a architectural related project in which I am supposed to draw a wall using the input coordinates given by mouse position.
I am using ScreenPointToRay(Input.mousePosition) to return with mouse coordinates and can create a line segment (line renderer) using the two Vector3 generated at two points of mouse click-drag.
Now, I need to put a wall instead of line, which should have all the properties like angle with correct dimensions.
using UnityEngine;
public class CubeMaker : MonoBehaviour
{
public new Camera camera;
public Mesh cubeMesh;
public Material cubeMaterial;
public float wallWidth;
public float depth;
public static Vector3 startpointer;
public static Vector3 endpointer;
private float x, y, z;
private Vector3? lineStartPoint;
private double wallthickness = 0.5;
public float angle;
void start()
{
camera = GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//getting the coordinates for mouse start point
lineStartPoint = GetMouseCameraPoint();
var linestartvec = GetMouseCameraPoint();
startpointer = linestartvec;
// Debug.Log("Line start point is" +lineStartPoint);
Debug.Log("startpoint is" + startpointer);
}
if (Input.GetMouseButtonUp(0))
{
//just a cross check if mouse start point has been taken or not
if (!lineStartPoint.HasValue)
{
return;
}
//getting the coordinates for mouse end point
var lineEndPoint = GetMouseCameraPoint();
//Debug.Log("Line end" + lineEndPoint);
endpointer = lineEndPoint;
Debug.Log("endpoint is" + endpointer);
//adding both vectors and dividing by 2 to get median or linear interpolation can be used for getting center point of wall
Vector3 vectoradd = (startpointer + endpointer) / 2;
Debug.Log("vectoradd" + vectoradd);
// assigning x y z magnitutes to scalar
x = vectoradd[0];
y = vectoradd[1];
z = vectoradd[2];
//this part is also bugsy as it should be able to check where the wall is created and should assign respected x and y values to look like a wall
if (x > z)
{
wallWidth = x;
depth = z;
Debug.Log("wall width is x" + wallWidth + "depth is z" + depth+ Mathf.Abs(x));
}
else
{
wallWidth = z;
depth = x;
Debug.Log("wall width is z " + wallWidth + "depth is x" + depth +Mathf.Abs(x));
}
var gameObject = new GameObject();
var meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.material = cubeMaterial;
var meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = cubeMesh;
//var boxcollider = gameObject.AddComponent<BoxCollider>();
gameObject.transform.position = new Vector3(x, 1, z);
gameObject.transform.localScale = new Vector3(wallWidth,2, depth);
//setting line start point to 0 for new line
lineStartPoint = null;
}
angle = Vector3.Angle(startpointer, endpointer);
}
private Vector3 GetMouseCameraPoint()
{
//assigning value of mouse position to this function
var ray = camera.ScreenPointToRay(Input.mousePosition);
return ray.origin + ray.direction * 8;
}
}
this is the code I am using…