Unity - 未找到子尺度继承的解决方案
Unity - No Found Solution to Child Scale Inheritance
刚接触 Unity,尝试过多种解决方案均未成功。我已经阅读了很多论坛帖子,但 none 对我有用。
我一直在尝试在运行时将游戏对象捕捉到其他游戏对象(例如将船舶部件装备到船舶上)。该脚本按预期工作,但子对象继续随父对象缩放。
我已经尝试附加缩放脚本,这些脚本可以根据父对象比例动态缩放子对象。我已经尝试逐步完成执行此操作的合乎逻辑的方式。我已经尝试使用空游戏对象进行分层修复,但似乎没有任何东西可以为我的子对象保持恒定的全局比例。也许有人能看到我遗漏的东西。
请忽略此时代码中可能存在的其他问题,除非它们直接影响缩放问题。但欢迎提出建议。有兴趣尽我所能。
-层次结构-
Ship (scale (2,2,2)) 父级
-Empty Object (scale (0.5,0.5,0.5)) 这是设置为与可捕捉对象相同比例的父对象
public class Interactable : MonoBehaviour
{
public Transform pickUpDestination;
public Transform snapDestination;
public Vector3 snapPointPos;
private bool isSnapped;
public bool isClicked;
public float unsnapMouseDist = 0.5f;
public float distCheck;
private void FixedUpdate()
{
distCheck = Vector3.Distance(pickUpDestination.position, snapPointPos);
if (distCheck >= unsnapMouseDist && isClicked)
{
isSnapped = false;
transform.SetParent(pickUpDestination);
transform.position = transform.parent.position;
Debug.Log("UNSNAPPED");
}
else if (distCheck <= unsnapMouseDist && isClicked)
{
isSnapped = true;
transform.SetParent(snapDestination, true);
transform.position = transform.parent.position;
Debug.Log("SNAPPED!");
}
if (transform.parent != null)
{
Debug.Log("Parent =" + transform.parent.name);
}
Debug.Log("Global Scale = " + transform.lossyScale);
Debug.Log("Local Scale = " + transform.localScale);
}
void OnMouseDown()
{
isClicked = true;
Debug.Log("Clicked = " + isClicked);
if (isSnapped == false)
{
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().isKinematic = true;
transform.SetParent(pickUpDestination);
}
}
void OnTriggerEnter(Collider snap)
{
if (snap.gameObject.CompareTag("Snap Point"))
{
snapPointPos = snap.transform.position;
snapDestination = snap.transform;
transform.rotation = snap.transform.rotation;
}
}
void OnMouseUp()
{
isClicked = false;
Debug.Log("Clicked = " + isClicked);
if (isSnapped == false)
{
this.transform.parent = null;
GetComponent<Rigidbody>().isKinematic = false;
GetComponent<Rigidbody>().useGravity = true;
}
}
}
和缩放脚本:
public class Scaler : MonoBehaviour
{
public Transform parent;
private void FixedUpdate()
{
parent = transform.parent;
transform.localScale = new Vector3(transform.localScale.x / parent.localScale.x, transform.localScale.y / parent.localScale.y, transform.localScale.z / parent.localScale.z);
}
}
lossyScale
是所有父尺度对该对象的综合影响(一些注意事项但不太可能适用于此处)。
localScale
是仅该对象的缩放效果(您在变换检查器中看到的数字)
var trueScale == new Vector3(
desiredScale.x / parentTransform.lossyScale.x,
desiredScale.y / parentTransform.lossyScale.y,
desiredScale.z / parentTransform.lossyScale.z);
transform.localScale = trueScale;
刚接触 Unity,尝试过多种解决方案均未成功。我已经阅读了很多论坛帖子,但 none 对我有用。
我一直在尝试在运行时将游戏对象捕捉到其他游戏对象(例如将船舶部件装备到船舶上)。该脚本按预期工作,但子对象继续随父对象缩放。
我已经尝试附加缩放脚本,这些脚本可以根据父对象比例动态缩放子对象。我已经尝试逐步完成执行此操作的合乎逻辑的方式。我已经尝试使用空游戏对象进行分层修复,但似乎没有任何东西可以为我的子对象保持恒定的全局比例。也许有人能看到我遗漏的东西。
请忽略此时代码中可能存在的其他问题,除非它们直接影响缩放问题。但欢迎提出建议。有兴趣尽我所能。
-层次结构-
Ship (scale (2,2,2)) 父级
-Empty Object (scale (0.5,0.5,0.5)) 这是设置为与可捕捉对象相同比例的父对象
public class Interactable : MonoBehaviour
{
public Transform pickUpDestination;
public Transform snapDestination;
public Vector3 snapPointPos;
private bool isSnapped;
public bool isClicked;
public float unsnapMouseDist = 0.5f;
public float distCheck;
private void FixedUpdate()
{
distCheck = Vector3.Distance(pickUpDestination.position, snapPointPos);
if (distCheck >= unsnapMouseDist && isClicked)
{
isSnapped = false;
transform.SetParent(pickUpDestination);
transform.position = transform.parent.position;
Debug.Log("UNSNAPPED");
}
else if (distCheck <= unsnapMouseDist && isClicked)
{
isSnapped = true;
transform.SetParent(snapDestination, true);
transform.position = transform.parent.position;
Debug.Log("SNAPPED!");
}
if (transform.parent != null)
{
Debug.Log("Parent =" + transform.parent.name);
}
Debug.Log("Global Scale = " + transform.lossyScale);
Debug.Log("Local Scale = " + transform.localScale);
}
void OnMouseDown()
{
isClicked = true;
Debug.Log("Clicked = " + isClicked);
if (isSnapped == false)
{
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().isKinematic = true;
transform.SetParent(pickUpDestination);
}
}
void OnTriggerEnter(Collider snap)
{
if (snap.gameObject.CompareTag("Snap Point"))
{
snapPointPos = snap.transform.position;
snapDestination = snap.transform;
transform.rotation = snap.transform.rotation;
}
}
void OnMouseUp()
{
isClicked = false;
Debug.Log("Clicked = " + isClicked);
if (isSnapped == false)
{
this.transform.parent = null;
GetComponent<Rigidbody>().isKinematic = false;
GetComponent<Rigidbody>().useGravity = true;
}
}
}
和缩放脚本:
public class Scaler : MonoBehaviour
{
public Transform parent;
private void FixedUpdate()
{
parent = transform.parent;
transform.localScale = new Vector3(transform.localScale.x / parent.localScale.x, transform.localScale.y / parent.localScale.y, transform.localScale.z / parent.localScale.z);
}
}
lossyScale
是所有父尺度对该对象的综合影响(一些注意事项但不太可能适用于此处)。
localScale
是仅该对象的缩放效果(您在变换检查器中看到的数字)
var trueScale == new Vector3(
desiredScale.x / parentTransform.lossyScale.x,
desiredScale.y / parentTransform.lossyScale.y,
desiredScale.z / parentTransform.lossyScale.z);
transform.localScale = trueScale;