1
RayCast
을 통해 개체를 배치 할 수 있지만 서로 내부에 개체를 배치하고 있습니다. 이 충돌을 감지하여 객체가 그 아래에있는 경우 인 경우 새 객체를 그 위에 놓아야합니다.. 또는 왼쪽에 충돌하는 경우 오른쪽에 객체를 놓아야합니다.개체를 다른 개체 위에 배치하려면 어떻게해야합니까?
여기에 내 코드
using UnityEngine;
using System.Collections;
public class targetPosition : MonoBehaviour {
public GameObject obj;
Vector3 newPosition;
void Start() {
newPosition = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0)) //It places cube on left click
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
newPosition = hit.point;
GameObject obj1 = Instantiate(obj,newPosition, Quaternion.identity) as GameObject;
obj1.SetActive (true);
}
}
if (Input.GetMouseButtonDown (1)) //deletes cube on right click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Destroy(hit.collider.gameObject);
}
}
}
}
감사합니다.
안녕하세요. colliders를 살펴보아야합니다. https://docs.unity3d.com/ScriptReference/Collider.html. 트리거로도 사용할 수 있음을 기억하십시오 (프로젝트에서이 중 일부는 필요합니다). 그래서 큐브 배치에, 당신은 마녀 트리거가 사용되었는지 확인하고, 큐브를 움직이거나 큐브를 움직여야합니다. –