Hello
I want to say that stupid write “infinity cycle” but…
I looked this tutotial https://www.youtube.com/watch?v=n2fx9rkMMVU and all works fine, this is code:
using UnityEngine;
using System.Collections;
public class CreateWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
public GameObject WallPrefab;
GameObject Wall;
private Camera camera;
//13 is the Layer number I set to "IgnoreRaycast" in the Inspector.
void Start () {
camera = GetComponent<Camera>();
}
void Update () {
GetInput ();
}
void GetInput () {
if (Input.GetMouseButtonDown (0)) {
Setstart ();
start.GetComponent<Renderer>().enabled = true;
end.GetComponent<Renderer>().enabled = true;
} else if (Input.GetMouseButtonUp (0)) {
SetEnd ();
start.GetComponent<Renderer>().enabled = false;
end.GetComponent<Renderer>().enabled = false;
} else {
if (creating) {
Adjust ();
}
}
}
Vector3 gridSnap(Vector3 originalPosition){
int granularity = 1;
Vector3 snappedPosition = new Vector3(Mathf.Floor(originalPosition.x / granularity) * granularity, originalPosition.y, Mathf.Floor(originalPosition.z / granularity) * granularity);
return snappedPosition;
}
void Setstart () {
creating = true;
start.transform.position = gridSnap(GetWorldPoint ());
Wall = (GameObject)Instantiate(WallPrefab, start.transform.position, Quaternion.identity);
}
void SetEnd () {
creating = false;
end.transform.position = gridSnap(GetWorldPoint ());
}
void Adjust () {
end.transform.position = gridSnap(GetWorldPoint ());
AdjustWall ();
}
void AdjustWall () {
float dist = Vector3.Distance (start.transform.position, end.transform.position);
start.transform.LookAt (end.transform.position);
end.transform.LookAt (start.transform.position);
Wall.transform.position = start.transform.position + dist / 2 * start.transform.forward;
Wall.transform.rotation = start.transform.rotation;
Wall.transform.localScale = new Vector3 (Wall.transform.localScale.x, Wall.transform.localScale.y, dist);
}
Vector3 GetWorldPoint () {
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
return hit.point;
}
return Vector3.zero;
//Using Vector3.zero cuased it to glitch look at 0,0,0.
}
}
BUT, The wall is scaling the distance between the start position and end position. And I do not have one object as one wall. So I thought i will create more same object adjacently than one scaled object…
My idea was something like this:
float dist = Vector3.Distance (start.transform.position, end.transform.position);
float Count = dist / WallPrefab.GetComponent<Renderer>().bounds.size.z;
for(int i = 0; i <= Count; i++){
Wall[i] = (GameObject)Instantiate(WallPrefab, start.transform.position, Quaternion.identity);
i = i++;
}
Here I have a mistake because the cycle is repeated to infinity… I feel stupid but it should not stop when the cycle is to achieve a greater value than the “Count”?