My objects are instantiating at x axis 0 and not where I click. As you can see in the below first image that when I Click at a point on the game screen and drag the wall (shown in Purple) from that point, after I let go of the wall at point B, the actual objects that I want are not instantiated at the wall position and along the length of the wall. Instead, you can see in the second image that the objects (shown in Purple and Yellow) are instantiated on and from a point I did not click. Is there any way to solve this? I have attached my script below. Please I need help. Thank you.
ShowMousePosition pointer;
bool creating;
public GameObject start;
public GameObject end;
public GameObject wallPrefab;
GameObject wall;
public GameObject polePrefab;
public GameObject fencePrefab;
GameObject lastPole;
bool xSnapping;
bool zSnapping;
Vector3 startPos;
Vector3 _start;
Vector3 _end;
Vector3 _dir;
Vector3 pos;
int wallLength;
// Use this for initialization
void Start () {
pointer = GetComponent<ShowMousePosition> ();
}
// Update is called once per frame
void Update () {
getInput();
}
void getInput() {
if(Input.GetMouseButtonDown(0)) {
setStart();
if (zSnapping.Equals (xSnapping)) {
zSnapping = true;
} else {
zSnapping = false;
}
if (xSnapping.Equals (zSnapping)) {
xSnapping = true;
} else {
xSnapping = false;
}
} else if(Input.GetMouseButtonUp(0)) {
setEnd ();
Destroy (wall);
updateFence ();
} else {
if(creating) {
adjust();
//updateFence ();
}
}
}
void setStart() {
Camera cam = GetComponent<Camera> ();
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
_start = hit.point;
//Vector3 startPos = pointer.getWorldPoint ();
creating = true;
start.transform.position = pointer.snapPosition (_start);
wall = (GameObject)Instantiate (wallPrefab, start.transform.position, Quaternion.identity);
}
}
void setEnd() {
//Vector3 startPos = pointer.getWorldPoint ();
creating = false;
Vector3 startPos = pointer.getWorldPoint ();
startPos = pointer.snapPosition (startPos);
GameObject startPole = Instantiate (polePrefab, startPos, Quaternion.identity);
startPole.transform.position = new Vector3 (pos.x, pos.y, pos.z);
//startPole.transform.position = new Vector3 (start.transform.position.x, startPos.y + 0f, start.transform.position.z);
lastPole = startPole;
//updateFence ();
}
void updateFence() {
_dir = (_end - _start).normalized;
for (int i = 0; i < wallLength; i++) {
Vector3 pos = _dir * i;
Vector3 worldPoint = pointer.getWorldPoint ();
worldPoint = pointer.snapPosition (worldPoint);
worldPoint = new Vector3 (Mathf.Floor(pos .x), pos.y + 0f, (Mathf.Floor(pos .z)));
if (!pos.Equals (lastPole.transform.position)) {
createFenceSegment (pos);
}
}
}
void createFenceSegment(Vector3 current) {
GameObject newPole = Instantiate (polePrefab, current, Quaternion.identity);
float distance = Vector3.Distance (newPole.transform.position, lastPole.transform.position);
Vector3 middle = Vector3.Lerp (newPole.transform.position, lastPole.transform.position, 0.5f);
GameObject newFence = Instantiate (fencePrefab, middle, Quaternion.identity);
newPole.transform.rotation = newFence.transform.rotation;
//newFence.transform.localScale = new Vector3 (newFence.transform.calScale.x, newFence.transform.localScale.y, distance);
newFence.transform.LookAt (lastPole.transform);
lastPole = newPole;
}
void adjust() {
//Vector3 startPos = pointer.getWorldPoint ();
_end = pointer.getWorldPoint ();
Camera cam = GetComponent<Camera> ();
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
_end = hit.point;
wallLength = Mathf.FloorToInt (Vector3.Distance (_start, _end));
end.transform.position = pointer.snapPosition(_end);
}
if(xSnapping) {
end.transform.position = new Vector3(start.transform.position.x, end.transform.position.y, end.transform.position.z);
}
if(zSnapping) {
end.transform.position = new Vector3(end.transform.position.x, end.transform.position.y, start.transform.position.z);
}
adjustWall();
}
void adjustWall() {
start.transform.LookAt(end.transform.position);
end.transform.LookAt(start.transform.position);
float distanceo = Vector3.Distance(start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distanceo/2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3(wall.transform.localScale.x, wall.transform.localScale.y, distanceo);
}