0

I have an object (orange) that is tied to a hinge joint (black) and can rotate around it. At some point when I detect collision with other object I have to cut it there (red). In order to do that I want to shrink the object between those points (black and red), but I need it to remain tied to the hinge joint and to keep its width. enter image description here

void OnCollisionEnter2D(Collision2D collision)
{
    GameObject obj = collision.gameObject;
    Vector3 endPos =collision.contacts[0].point;
    float scaleX = Vector3.Distance(new Vector3(startPos.x, 0, 0), new Vector3(endPos.x, 0, 0));
    float scaleY = Vector3.Distance(new Vector3(0, startPos.y, 0), new Vector3(0, endPos.y, 0));
    Vector3 newScale = new Vector3(scaleX,scaleY, 1);
    obj.transform.parent.transform.localScale = obj.transform.TransformPoint(newScale);
}

And the start position is initialized by the position of the hinge joint:

startPos = GameObject.Find("hair").GetComponent<HingeJoint2D>().transform.position;

I wrapped the object in parent and scaled it, because I read somewhere that then it would remain in place:

enter image description here

I tried many things, but I can’t get it to stay in place and be in right size. How can I implement it right?

4
  • What is startPos? How are the transform components of the orange object and its parent nodes arranged? Is it a simple sprite or something else? How do you want to shrink it? It looks like a curly stick, but I saw you scaled both x and y coords of the parent's transform, I don't understand this. Commented Jun 4, 2024 at 9:10
  • I added the details in the question. And I do use the y, because the object may be rotated to any angle. how else should I calculate it's new scale? @shingo Commented Jun 4, 2024 at 9:24
  • Have you tried to change only y scale and the pivot of the sprite? Can you add more details like the position of each node, and where the connected rigidbody is? Commented Jun 4, 2024 at 11:26
  • 1
    Your problem at its core stems from the pivot point of that object, as @azelito pointed out. If you set that right, scaling works as you expect it. Understanding pivot points (or anchors) will be useful when working with the UI too. Commented Jun 4, 2024 at 20:57

1 Answer 1

2

If you move the pivot (position) of the parent transform to the hinge, then you apply the localScale to the parent object you will achieve the desired result.

enter image description here

Example:

private float scale = 3;

And then:

transform.parent.transform.localScale = new Vector3(transform.parent.transform.localScale.x, transform.parent.transform.localScale.y, scale);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I didn't position the parent object right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.