Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Drawing a wall(or cube primitive with wall dimensions) from a line segment, inheriting its propertie

Discussion in 'Scripting' started by KushMathur, Jul 16, 2019.

  1. KushMathur

    KushMathur

    Joined:
    Jul 3, 2019
    Posts:
    22
    :)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.

    Code (CSharp):
    1. using UnityEngine;
    2. public class CubeMaker : MonoBehaviour
    3. {
    4.         public new Camera camera;
    5.         public Mesh cubeMesh;
    6.         public Material cubeMaterial;
    7.         public float wallWidth;
    8.         public float depth;
    9.         public static Vector3 startpointer;
    10.         public static Vector3 endpointer;
    11.         private float x, y, z;
    12.         private Vector3? lineStartPoint;
    13.         private double wallthickness = 0.5;
    14.         public float angle;
    15.      
    16.         void start()
    17.         {
    18.             camera = GetComponent<Camera>();
    19.         }
    20.         void Update()
    21.         {
    22.             if (Input.GetMouseButtonDown(0))
    23.             {
    24.                 //getting the coordinates for mouse start point
    25.                 lineStartPoint = GetMouseCameraPoint();
    26.                 var linestartvec = GetMouseCameraPoint();
    27.                 startpointer = linestartvec;
    28.                // Debug.Log("Line start point is" +lineStartPoint);
    29.                 Debug.Log("startpoint is" + startpointer);
    30.             }
    31.             if (Input.GetMouseButtonUp(0))
    32.             {
    33.                 //just a cross check if mouse start point has been taken or not
    34.                 if (!lineStartPoint.HasValue)
    35.                 {
    36.                     return;
    37.                 }
    38.  
    39.                 //getting the coordinates for mouse end point
    40.                 var lineEndPoint = GetMouseCameraPoint();
    41.                 //Debug.Log("Line end" + lineEndPoint);
    42.                 endpointer = lineEndPoint;
    43.                 Debug.Log("endpoint is" + endpointer);
    44.              
    45.                 //adding both vectors and dividing by 2 to get median or linear interpolation can be used for getting center point of wall
    46.                 Vector3 vectoradd = (startpointer + endpointer) / 2;
    47.                 Debug.Log("vectoradd" + vectoradd);
    48.                 // assigning x y z magnitutes to scalar
    49.                 x = vectoradd[0];
    50.                 y = vectoradd[1];
    51.                 z = vectoradd[2];
    52.                     //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
    53.                     if (x > z)
    54.                     {
    55.                         wallWidth = x;
    56.                         depth = z;
    57.                         Debug.Log("wall width is x" + wallWidth + "depth is z" + depth+ Mathf.Abs(x));
    58.                     }
    59.                     else
    60.                     {
    61.                         wallWidth = z;
    62.                         depth = x;
    63.                         Debug.Log("wall width is z " + wallWidth + "depth is x" + depth +Mathf.Abs(x));
    64.                     }
    65.                 var gameObject = new GameObject();
    66.                 var meshRenderer = gameObject.AddComponent<MeshRenderer>();
    67.                 meshRenderer.material = cubeMaterial;
    68.                 var meshFilter = gameObject.AddComponent<MeshFilter>();
    69.                 meshFilter.mesh = cubeMesh;
    70.                 //var boxcollider = gameObject.AddComponent<BoxCollider>();
    71.                 gameObject.transform.position = new Vector3(x, 1, z);
    72.                 gameObject.transform.localScale = new Vector3(wallWidth,2, depth);
    73.              
    74.                 //setting line start point to 0 for new line
    75.                 lineStartPoint = null;
    76.             }
    77.  
    78.         angle = Vector3.Angle(startpointer, endpointer);
    79.         }
    80.  
    81.         private Vector3 GetMouseCameraPoint()
    82.         {
    83.             //assigning value of mouse position to this function
    84.             var ray = camera.ScreenPointToRay(Input.mousePosition);
    85.             return ray.origin + ray.direction * 8;
    86.         }
    87. }    
    this is the code I am using...:)
     
    Last edited: Jul 16, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    You should build a mesh along that line.
     
  3. KushMathur

    KushMathur

    Joined:
    Jul 3, 2019
    Posts:
    22
    Yes, After I get the two points from mouse position, I add mesh filter and mesh renderer to it to shape it into any primitive.

    Or did you meant something else?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Mesh filter and renderer are required to render the mesh on the screen. You need to generate geometry and push it to the filter and assign a material to the renderer.
     
  5. KushMathur

    KushMathur

    Joined:
    Jul 3, 2019
    Posts:
    22
    Untitled.jpg Untitlede.jpg
    Yes, I did that too. As you can see in the screenshots, The first one is perfect which I wish to have. But in second image, when the position of the player changes, the size and dimensions of wall also changes. I clicked on the two spots marked as red on the game, but the cube that is created is not what in the right position.

    In this case, I think the angle between the two vectors are responsible.