简体   繁体   English

Unity2d:如何从子对象中旋转父对象?

[英]Unity2d: How to rotate a parent object from within a child object?

I am building a UI that allows an object to be dragged and rotated. 我正在构建允许拖动和旋转对象的UI。

The rotation script works, however when attaching to a child element (UI arrow icon) I cannot move the parent. 旋转脚本有效,但是在附加到子元素(UI箭头图标)时,我无法移动父元素。 The parent object is referenced correctly and is outputted to log. 正确引用了父对象并将其输出到日志。 Do I have to reference some type of ridge body? 我是否必须参考某种类型的山脊体?

CHILD ROTATION CLASS 儿童旋转课

public class DragRotate : MonoBehaviour
{
    [Header("Degree of rotation offset. *360")]
    [Header("Component must be located inside a Control layer - (parent.parent)")]

    public float offset = 0.0f;

    Vector3 startDragDir;
    Vector3 currentDragDir;
    Quaternion initialRotation;
    float angleFromStart;

    //declare parent object
    private GameObject myParent;

    void Start()
    {
        //Set parent object two layers up
        myParent = transform.parent.parent.gameObject;
        Debug.Log("Rotation Object Parent: " + myParent);
    }

    void OnMouseDown()
    {
        //get initial coordinance of object and mouse
        startDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - myParent.transform.position;

        //set initial rotation variable
        initialRotation = myParent.transform.rotation;
    }

    void OnMouseDrag()
    {

        //calculate mouse vs object location
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - myParent.transform.position;

        //convert the vector to one plane
        difference.Normalize();

        //trig calculation for radian to degree conversion
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

        //set rotation
        myParent.transform.rotation = Quaternion.Euler(0f, 0f, rotationZ - (90 + offset));
    }
}

PARENT MOVEMENT CLASS (EVERYTHING WORKS FINE HERE) 父级移动课程(此处一切正常)

[RequireComponent(typeof(BoxCollider2D))]
public class DragComponent : MonoBehaviour
{

    [Header("Can be dragged by player?")]
    [Tooltip("Disable from player being able to drag?")]
    private bool canBeDragged = true;

    private Vector3 screenPoint;
    private Vector3 offset;

    void OnMouseDown()
    {
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

        Debug.Log("Start Drag of: " + transform.name);
    }

    void OnMouseDrag()
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
    }
}

You don't need to store a reference to the parent object. 您不需要存储对父对象的引用。 You can just use it on the go like this: 您可以像这样在旅途中使用它:

void OnMouseDown()
{
    initialRotation = transform.parent.rotation;
} 

void OnMouseDrag()
{
    transform.parent.rotation = Quaternion.Euler(0f, 0f, rotationZ - (90 + offset));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM