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 FenceInput : MonoBehaviour {
- public GameObject FencePostPrefab;
- // Update is called once per frame
- void Update () {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if(Physics.Raycast(ray, out hit))
- {
- FencePostPrefab.transform.position = ConvertWorldToPlanePoint(hit);
- }
- }
- private Vector2 GetPlaneWidthHeight(GameObject plane)
- {
- Mesh planeMesh = plane.GetComponent<MeshFilter>().mesh;
- Bounds bounds = planeMesh.bounds;
- float boundsX = plane.transform.localScale.x * bounds.size.x;
- float boundsZ = plane.transform.localScale.z * bounds.size.z;
- return new Vector2(boundsX, boundsZ);
- }
- private Vector3 ConvertWorldToPlanePoint(RaycastHit hit)
- {
- Vector2 planeSize = GetPlaneWidthHeight(hit.transform.gameObject);
- float width = planeSize.x / 10;
- float height = planeSize.y / 10;
- int x = ((int)(hit.point.x / 10)) * 10;
- int z = ((int)(hit.point.z / 10)) * 10;
- return new Vector3(
- Mathf.Round(hit.point.x * 10)
- ,hit.point.y,
- Mathf.Round(hit.point.z * 10));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement