Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlacementSystem : MonoBehaviour
- {
- /* public float placementDistance = 5f;
- public LayerMask placementLayerMask;
- public Material validPlacementMaterial;
- public Material invalidPlacementMaterial;
- private GameObject placementIndicator;
- private bool canPlace = false;
- private Vector3 placementPosition;
- private Quaternion placementRotation;
- private bool isRotating = false;
- void Start()
- {
- placementIndicator = GameObject.CreatePrimitive(PrimitiveType.Cube);
- placementIndicator.GetComponent<Collider>().enabled = false;
- placementIndicator.GetComponent<Renderer>().material = validPlacementMaterial;
- placementIndicator.SetActive(false);
- }
- void Update()
- {
- if (InteractionSystem.Instance.IsPrecisionDropEnabled())
- {
- UpdatePlacementIndicator();
- HandleRotation();
- }
- else
- {
- placementIndicator.SetActive(false);
- }
- }
- void UpdatePlacementIndicator()
- {
- RaycastHit hit;
- if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, placementDistance, placementLayerMask))
- {
- placementPosition = hit.point + hit.normal * 0.05f;
- placementRotation = Quaternion.LookRotation(hit.normal, Vector3.up);
- placementIndicator.transform.position = placementPosition;
- placementIndicator.transform.rotation = placementRotation;
- placementIndicator.SetActive(true);
- // Check if the placement is valid
- Collider[] colliders = Physics.OverlapBox(placementPosition, placementIndicator.transform.localScale / 2, placementRotation, placementLayerMask);
- canPlace = colliders.Length == 0;
- placementIndicator.GetComponent<Renderer>().material = canPlace ? validPlacementMaterial : invalidPlacementMaterial;
- }
- else
- {
- placementIndicator.SetActive(false);
- canPlace = false;
- }
- }
- void HandleRotation()
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- isRotating = !isRotating;
- }
- if (isRotating)
- {
- if (Input.GetKeyDown(KeyCode.Q))
- {
- placementRotation *= Quaternion.Euler(0, -45, 0);
- }
- else if (Input.GetKeyDown(KeyCode.E))
- {
- placementRotation *= Quaternion.Euler(0, 45, 0);
- }
- }
- }
- public bool CanPlace()
- {
- return canPlace;
- }
- public Vector3 GetPlacementPosition()
- {
- return placementPosition;
- }
- public Quaternion GetPlacementRotation()
- {
- return placementRotation;
- } */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement