英文:
How would I go about placing a GameObject on another object's surface while matching the rotation? Unity3D
问题
I'm making a game where you build, and when I try to place a GameObject on another one's surface it uses global rotation and not local, however I'm trying to get the placed GameObject's rotation to match the other one. This is hard because I also want to have the placed GameObject's be facing away from the surface, if that makes sense.
I've tried using a raycast hit normal to get the placed GameObject facing the right way, and that works only partly. Its up direction is correct, but the other axis's are not orientated to match the other object. Here's the code:
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, buildingRange, buildable.value))
{
//Indicator to show where the item will be placed
buildBall.SetActive(true);
buildBall.transform.position = hit.point;
if (Input.GetMouseButtonDown(0))
{
//Create the item
GameObject buildItem = Instantiate(currentItem, Vector3.zero, Quaternion.identity);
//Set the item's position
buildItem.transform.position = hit.point;
//Rotate the item so that it faces away from the other object
buildItem.transform.up = hit.normal;
{Connect the buildItem object to the hit.collider.gameObject}
}
}
I've spent many hours trying numerous solutions, this one yields the closest result.
Here are some pics:
This is what is intended, all blocks are orientated correctly:
However when the mass rotates, the blocks that are added on are orientated incorrectly:
This got my whole family stumped, but it's gotta be possible.
英文:
I'm making a game where you build, and when I try to place a GameObject on another one's surface it uses global rotation and not local, however I'm trying to get the placed GameObject's rotation to match the other one. This is hard because I also want to have the placed GameObject's be facing away from the surface, if that makes sense.
I've tried using a raycast hit normal to get the placed GameObject facing the right way, and that works only partly. Its up direction is correct, but the other axis's are not orientated to match the other object. Here's the code:
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, buildingRange, buildable.value))
{
//Indicator to show where the item will be placed
buildBall.SetActive(true);
buildBall.transform.position = hit.point;
if (Input.GetMouseButtonDown(0))
{
//Create the item
GameObject buildItem = Instantiate(currentItem, Vector3.zero, Quaternion.identity);
//Set the item's position
buildItem.transform.position = hit.point;
//Rotate the item so that it faces away from the other object
buildItem.transform.up = hit.normal;
{Connect the buildItem object to the hit.collider.gameObject}
}
}
I've spent many hours trying numerous solutions, this one yields the closest result.
Here are some pics:
This is what is intended, all blocks are orientated correctly:
However when the mass rotates, the blocks that are added on are orientated incorrectly:
This got my whole family stumped, but it's gotta be possible.
答案1
得分: 1
你需要对其他轴线进行对齐。
因此,您可以使用 hit.normal 作为 GameObject 的新上方向,然后基于此计算其他轴线:
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, buildingRange, buildable.value))
{
// 用于显示物品将被放置的指示器
buildBall.SetActive(true);
buildBall.transform.position = hit.point;
if (Input.GetMouseButtonDown(0))
{
// 创建物品
GameObject buildItem = Instantiate(currentItem, Vector3.zero, Quaternion.identity);
// 设置物品的位置
buildItem.transform.position = hit.point;
// 基于 hit.normal 和参考方向计算旋转
Vector3 referenceDirection = Vector3.up; // 您可以根据需要更改此参考方向
Quaternion rotation = Quaternion.LookRotation(hit.normal, referenceDirection);
// 将旋转应用于 buildItem
buildItem.transform.rotation = rotation;
// 将 buildItem 对象连接到 hit.collider.gameObject
}
}
这是您提供的代码的中文翻译。
英文:
You need to align the other axes as well.
So you could use the hit.normal as the new up direction for the GameObject, and then calculate the other axes based on that:
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, buildingRange, buildable.value))
{
// Indicator to show where the item will be placed
buildBall.SetActive(true);
buildBall.transform.position = hit.point;
if (Input.GetMouseButtonDown(0))
{
// Create the item
GameObject buildItem = Instantiate(currentItem, Vector3.zero, Quaternion.identity);
// Set the item's position
buildItem.transform.position = hit.point;
// Calculate the rotation based on the hit.normal and a reference direction
Vector3 referenceDirection = Vector3.up; // You can change this to your desired reference direction
Quaternion rotation = Quaternion.LookRotation(hit.normal, referenceDirection);
// Apply the rotation to the buildItem
buildItem.transform.rotation = rotation;
// Connect the buildItem object to the hit.collider.gameObject
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论