Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FenceInput : MonoBehaviour {
  6.  
  7. public GameObject FencePostPrefab;
  8.  
  9.  
  10.  
  11. // Update is called once per frame
  12. void Update () {
  13.  
  14. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  15.  
  16. RaycastHit hit;
  17.  
  18.  
  19. if(Physics.Raycast(ray, out hit))
  20. {
  21. FencePostPrefab.transform.position = ConvertWorldToPlanePoint(hit);
  22. }
  23. }
  24.  
  25. private Vector2 GetPlaneWidthHeight(GameObject plane)
  26. {
  27. Mesh planeMesh = plane.GetComponent<MeshFilter>().mesh;
  28.  
  29. Bounds bounds = planeMesh.bounds;
  30. float boundsX = plane.transform.localScale.x * bounds.size.x;
  31. float boundsZ = plane.transform.localScale.z * bounds.size.z;
  32.  
  33. return new Vector2(boundsX, boundsZ);
  34.  
  35. }
  36.  
  37. private Vector3 ConvertWorldToPlanePoint(RaycastHit hit)
  38. {
  39. Vector2 planeSize = GetPlaneWidthHeight(hit.transform.gameObject);
  40.  
  41. float width = planeSize.x / 10;
  42. float height = planeSize.y / 10;
  43.  
  44. int x = ((int)(hit.point.x / 10)) * 10;
  45. int z = ((int)(hit.point.z / 10)) * 10;
  46.  
  47.  
  48.  
  49. return new Vector3(
  50. Mathf.Round(hit.point.x * 10)
  51. ,hit.point.y,
  52. Mathf.Round(hit.point.z * 10));
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement