Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class HightLight : MonoBehaviour
- {
- public GameObject HightLightPrefab;
- public List<HightLightHelper> MyHightlightsList;
- public Vector3 firstTilePoint = Vector3.zero;
- private bool isxBackwards = false;
- private bool iszBackwards = false;
- // Start is called before the first frame update
- void Awake()
- {
- MyHightlightsList = new List<HightLightHelper>();
- }
- // Update is called once per frame
- void Update()
- {
- int hightLightTileWidth = 0;
- int hightLightTileHeight = 0;
- Vector3 cuserTilePosition;
- RaycastHit hit;
- var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(Ray, out hit, 1000))
- {
- Vector3 hitPoint = hit.point;
- cuserTilePosition = new Vector3(GetTilePointsX(hitPoint.x), 0, GetTilePointsZ(hit.point.z));
- if (Input.GetMouseButtonDown(0))
- {
- firstTilePoint = cuserTilePosition;
- }
- ClearTheHightLightList();
- if (Input.GetMouseButton(0))
- {
- hightLightTileWidth =( GetTilePointsX(hitPoint.x) - (int)firstTilePoint.x) + 1;
- hightLightTileHeight =( GetTilePointsZ(hitPoint.z) - (int)firstTilePoint.z) + 1;
- if (hightLightTileHeight < 0)
- {
- iszBackwards = true;
- }
- else
- {
- iszBackwards = false;
- }
- if (hightLightTileWidth < 0)
- {
- isxBackwards = true;
- }
- else
- {
- isxBackwards = false;
- }
- hightLightTileHeight = Mathf.Abs(hightLightTileHeight);
- hightLightTileWidth = Mathf.Abs(hightLightTileWidth);
- //Debug.Log(hightLightTileWidth + " " + hightLightTileHeight);
- Debug.Log(isxBackwards + " " + iszBackwards);
- Vector3 newPlacePosition = Vector3.zero;
- for (int x = 0; hightLightTileWidth > x; x++)
- {
- for (int z = 0; hightLightTileHeight > z; z++)
- {
- if(isxBackwards == false && iszBackwards == false)
- {
- newPlacePosition = new Vector3(firstTilePoint.x + x, 0, firstTilePoint.z + z);
- }
- else if(isxBackwards == true && iszBackwards == true )
- {
- newPlacePosition = new Vector3(cuserTilePosition.x + x , 0, cuserTilePosition.z + z );
- }
- else
- if (isxBackwards == true && iszBackwards == false)
- {
- newPlacePosition = new Vector3(cuserTilePosition.x + x + 2 , 0, firstTilePoint.z + z);
- }
- else if(isxBackwards == false && iszBackwards == true)
- {
- newPlacePosition = new Vector3(firstTilePoint.x + x, 0, firstTilePoint.z + z);
- }
- PlaceHightDown(newPlacePosition);
- }
- }
- }
- this.transform.localPosition = cuserTilePosition;
- if (Input.GetKey(KeyCode.Escape))
- {
- ClearTheHightLightList();
- }
- }
- }
- private void ClearTheHightLightList()
- {
- for (int i = 0; i < MyHightlightsList.Count; i++)
- {
- HightLightHelper helperObject = MyHightlightsList[i];
- MyHightlightsList[i].Destroy();
- MyHightlightsList.Remove(helperObject);
- }
- }
- private void PlaceHightDown(Vector3 tileSpace)
- {
- GameObject go = GameObject.Instantiate(HightLightPrefab);
- HightLightHelper h = go.GetComponent<HightLightHelper>();
- h.HightLight.transform.localPosition = tileSpace;
- MyHightlightsList.Add(h);
- }
- private int GetTilePointsX(float worldPointx)
- {
- return (int)worldPointx;
- }
- private int GetTilePointsZ(float worldPointz)
- {
- return (int)worldPointz;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement