using System.Collections; using System.Collections.Generic; using UnityEngine; public class CreateFance : MonoBehaviour { bool creating = false; ShowMousePosition pointer; public GameObject polePrefab; public GameObject fencePrefab; GameObject lastPole; // Start is called before the first frame update void Start() { pointer = GetComponent(); } // Update is called once per frame void Update() { getInput(); } void getInput() { if(Input.GetMouseButtonDown(0)) { startFence(); } else if (creating) { updateFence(); } if (Input.GetMouseButtonUp(0)) creating = false; } void startFence() { creating = true; var startPos = pointer.getWorldPoint(); startPos = pointer.snapPosition(startPos); var startPole = Instantiate(polePrefab, startPos, Quaternion.identity); startPole.transform.position = new Vector3(startPos.x, startPos.y + 0.3f, startPos.z); lastPole = startPole; } void setFence() { creating = false; } void updateFence() { var current = pointer.getWorldPoint(); current = pointer.snapPosition(current); current = new Vector3(current.x, current.y + 0.3f, current.z); if(!current.Equals(lastPole.transform.position)) { CreateFenceSegment(current); } } void CreateFenceSegment(Vector3 current) { var newPole = Instantiate(polePrefab, current, Quaternion.identity); var middle = Vector3.Lerp(newPole.transform.position, lastPole.transform.position, 0.5f); var newFence = Instantiate(fencePrefab, middle, Quaternion.identity); newFence.transform.LookAt(lastPole.transform); lastPole = newPole; } }