Hi all!
I am trying to create a simple measurement script that will measure flat surfaces straight across. Currently, I am able to click left to declare a start point, and then hold down right and drag my line renderer around until I let go to see the length of that line.
However, I would like to only be able to measure at 90 degree intervals from my start point, similar to a snapping feature so the lines only render directly up, down, left, or right in order to get the most accurate readings.
Here is a visualization of how I would like it to work and my code so far. Appreciate any kind of help I can get as I have kind of hit a wall.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RigidMeasure : MonoBehaviour
{
public GameObject laserPrefab;
private GameObject laser;
private Transform laserTransform;
public GameObject marker;
private int count = 0;
private float dist;
private string final;
private float angle;
private Vector3 point1;
private Vector3 point2;
// Start is called before the first frame update
void Start()
{
laser = Instantiate(laserPrefab); //creates an instance of the laser prefab
laserTransform = laser.transform; //defines the laser transform
laser.SetActive(false);
}
// Update is called once per frame
void Update()
{
PlacePoint1();
PlacePoint2();
ClearMeasurement();
}
void PlacePoint1()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out RaycastHit hit, 100.0f);
if (Input.GetMouseButtonDown(0) && count == 0)
{
Instantiate(marker, hit.point, Quaternion.identity);
point1 = hit.point;
count++;
}
}
void PlacePoint2()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out RaycastHit hit, 100.0f);
if (Input.GetMouseButton(1) && count == 1)
{
point2 = hit.point;
DrawLaser();
}
if (Input.GetMouseButtonUp(1) && count == 1)
{
Instantiate(marker, hit.point, Quaternion.identity);
GeneratePrefabText();
count++;
AngleBetweenVector2(point1, point2);
}
}
void DrawLaser()
{
//If both points are declared
laser.SetActive(true); // Makes laser visible
Vector3 v1 = point1;
Vector3 v2 = point2;
// Place, orient and stretch measurement laser
laserTransform.position = Vector3.Lerp(v1, v2, .5f);
laserTransform.LookAt(v2);
dist = Vector3.Distance(v1, v2);
final = dist.ToString();
laserTransform.localScale = new Vector3(laserTransform.localScale.x, laserTransform.localScale.y, dist);
// Convert metric to imperial
float inches = dist * 39.36f;
int feet = 0;
bool end = false;
// Subtracts foot from total until remaining inches are found
while (end == false)
{
if (inches < 12)
{
end = true;
final = feet.ToString() + "'" + Math.Floor(inches).ToString() + '"';
}
else
{
feet++;
inches -= 12;
}
}
}
//creates a gameobject consisting of the correct number measurements from prefabs
void GeneratePrefabText()
{
GameObject letterCanvas = new GameObject();
letterCanvas.name = "NumberCanvas";
float offset = 0f;
foreach (char c in final)
{
string number;
GameObject character;
//converts the char that is read into a string that can be used to search the Resources/Numbers/Prefabs folder
if (c == '"')
{
number = "quote";
}
else if (c == '\'')
{
number = "apostrophe";
}
else
{
number = c.ToString();
}
//correctly instantiates each prefab and makes them all children of a canvas
character = Instantiate(Resources.Load("Numbers/Prefabs/" + number, typeof(GameObject)) as GameObject);
character.transform.position = new Vector3(offset, 4f, 0f);
character.transform.localScale = new Vector3(2f, 2f, 2f);
character.transform.Rotate(0, 180, 0);
character.transform.SetParent(letterCanvas.transform);
offset += 0.6f;
}
}
void ClearMeasurement()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("MeasurementTest");
}
}
private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = vec2 - vec1;
float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
Debug.Log(Vector2.Angle(Vector2.right, diference) * sign);
angle = Vector2.Angle(Vector2.right, diference) * sign;
return Vector2.Angle(Vector2.right, diference) * sign;
}
void FindAngleToSnap()
{
if ((AngleBetweenVector2(point1, point2) > -45f) && (AngleBetweenVector2(point1, point2) < 45f))
{
angle = 0;
}
else if ((AngleBetweenVector2(point1, point2) > 45f) && (AngleBetweenVector2(point1, point2) < 135f))
{
angle = 90;
}
else if ((AngleBetweenVector2(point1, point2) > 135f) && (AngleBetweenVector2(point1, point2) < -135f))
{
angle = 180;
}
else
{
angle = -90;
}
}
}