Question by
UncleGubbsy · Apr 08, 2016 at 01:04 PM ·
raycastvector3for-loop
Right Click and Drag Formation Line RTS
Hello, I have a question aobut my script. In this script you can Right click and drag, upon the release of right click it creates vector3s with an even distribution between all of them. My question is that the first vector3 created looks at the second raycast point. All of the other vector3s are added on the xPos of the first one. However, they are not adding from the first point, instead, they are adding from the world position, not local. Also cubes spawn at the vector3s to debug. So, how do I make the cubes Instantiate on each vector3 a certain amount of units away from the first vector3, locally.
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(1) && !firstClicked && Physics.Raycast(r, out hit)) //Right Clicked
{
firstRaycast = hit.point + hit.normal;
Debug.Log(firstRaycast + " First Raycast");
Debug.Log(firstClicked);
firstClicked = true;
}
RaycastHit hit2;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonUp(1) && firstClicked && Physics.Raycast(ray, out hit2)) //Right Clicked again
{
GameObject placeHolderInstance = Instantiate(placeHolder, firstRaycast, Quaternion.identity) as GameObject;
secondRaycast = hit2.point + hit2.normal;
placeHolderInstance.transform.LookAt(secondRaycast);
Debug.Log("Second raycast stored");
float distance = Vector3.Distance(firstRaycast, secondRaycast);
Debug.Log(firstRaycast + " First Raycast");
Debug.Log(distance + " Distance");
Debug.Log(unitsSelected.Count + " Units Selected");
float interval = distance / unitsSelected.Count;
for (int i = 1; i<unitsSelected.Count; i++)
{
Vector3 newPoint = new Vector3(firstRaycast.x + interval * i, firstRaycast.y, firstRaycast.z);
placeHolderInstance = Instantiate(placeHolder, newPoint, Quaternion.identity) as GameObject;
Debug.Log(interval + " Interval");
}
firstClicked = false;
}
Comment