Rotate a body about a given point Unity2d - unity3d

How to rotate a body relative to given dx dy in Unity? For example, I have a weapon sprite with a hand, and I was able to make it rotate relative to its center so that the weapon looks at the cursor, how can I rotate not relative to the center of the sprite, but relative to a given point?

The simplest way to achieve this is to set a gameobject at your desired point and make your object child of the created object. So when you rotate the parent, it will rotate relative to the parent point.

You can create an empty object and set the body as its child, then rotate the parent, the body will rotate around the parent like the parent be its pivot.
This is a practical way to set the pivot, and you can change it at any time.

Related

Some problem with rotation angles in unity?

I just attached one object to another. I don't understand what happened, but the rotation angles of the child object in the inspector or myChildObject.transform.rotation.eulerAngles (or myChildObject.transform.localRotation.eulerAngles) are different.
The Inspector shows the localRotation of objects, which will not change unless you change (or reparent) that specific object. Changing a parent's rotation or localRotation will never change its (existing) child's localRotation.
A child object with 0,0,0 Euler angles just means it isn't rotated compared to its parent -- i.e. its world rotation should match its parent's world rotation.
Rotating the parent should change the child's global rotation, but if the child does not have a localRotation of 0,0,0 they won't match. For example if the parent had a rotation of 45,0,0 and the child had a localRotation of 0,0,90, the child would end up with a global rotation of 45,0,90. (Things can get much weirder than that, though.)
To put it another way, the child's global rotation is its own localRotation combined with the rotation of its parent/s.

Rotate wheel collider object

I wanted to rotate a wheel collider object in unity.
I duplicated my back and front wheels objects to keep the position data.
It appeared perpendicular to the wheels. I cannot rotate it using the rotation tool or via the inspector.
As mentioned in this post: it could be solved using an empty game object for the rotation.
I tried to append it onto an Empty Game Object and rotate it. It did not work.
Thanks in advance
I had the same problem. The solution is simpler than the other answer suggests:
WheelCollider objects are dependent on the orientation of a rigid body they are attached to. Assuming that your bike has a rigid body, the solution is to make sure that the X-axis orientation of the bike matches the X-axis orientation of the wheel colliders.
Based on this post:
https://forum.unity.com/threads/unity-5-wheelcollider-wrong-rotation.349596/#post-2264801
WheelColliders always point in the "forward" and "down" direction as the rigidbody they're attached to. The steerAngle value is relative to the rigidbody's forward direction.
Please bear in mind, however, that your bike's model(the visuals that display bike's frame) might have a different orientation from the object that contains the rigidbody that wheel colliders are attached to.
In my case Hierarchy looked like so:
MainScene
CarRoot
CarModel
WheelColliders
Wheel_FR
Wheel_FL
Wheel_RR
Wheel_RL
The CarRoot had a rigid body on it. When I rotated it, that would also rotate my model. Which was not good. So I had to:
Unparent the CarModel and WheelColliders(made them children of MainScene)
Rotate the CarRoot by 90 degrees on the Y-axis(to align its X-axis with that of the WheelColliders)
Make CarModel and WheelColliders children of CarRoot again.
That did it for me.
1-) Create a capsule game object,
2-) Make it like whell.
3-) Delete capsule collider and add mesh collider, then select convex.,
Now you have a collider like whell.
4-) Put your whell game object under this, and fix sizes.
5-) Delete mesh filter and mesh renderer in capsule object.
6-) Use transform.rotate(vector3.right) or left on created base object.

Why am I getting an incorrect vector when trying to find HingeJoint2D.anchor in world space?

In the scene, I have a long chain of children that are connected via hinge to their parent. For my code, I need the position of the hinge anchors in world space, so I use:
public Vector2 hingeVector => hinge.anchor + (Vector2)gameObject.transform.position;
For the first hinge, that code gives the correct position. But for the second hinge this happens:
The red point is the vector I get, the blue point is the actual position. As you can see, it's a somewhat small but still problematic difference.
Is there any way I can fix this? I couldn't find anything like this online.
You need to add the object's rotation
The anchor values are axis aligned and aren't affected by rotation, but in order to calculate the anchor point in world space, knowing the transform's position, you need to rotate the anchor point values by the object's rotation then add it to the position:
Vector2 p = hinge.anchor.Rotate(gameObject.transform.rotation.eulerAngles.z)
+ (Vector2)gameObject.transform.position;

Does having Root Motion applied prevent moving a transform position relative to world coordinates?

I have "GameObject 0" (GO0) with an animation controller that used parameters to transform states and is used as a wrapper for "GameObject 1"(GO1). "GameObject 1" has an animation controller that has Root Motion applied. Within GO1 there are 3 game objects whos animation is in the same animation clip as GO1.
This allows me to place GO1 in world coordinates and animate GO1's children in local coordinates to maintain relative positioning.
However, I am wish to move one of the children of GO1 to a world coordinate that is not relative to GO1. I have used script and Vector3.Lerp / Vector3.MoveTowards functions, but they are not working as expected. Is the Root Motion interfering with the Lerps?
I have change the script to add a new parent to the child object and animating the new parent. This seems to work but the rotation of the new parent is causing a new issue.

How can find Center of circle to Unity?

I have circle, and I'm trying to find center and convert to Vector2.
For example:
circleLocation : new Rect( Screen.width/10, Screen.height/2.75, Screen.width/3,Screen.width/3);
centerofcirle:New Vector2(circleLocation.width/3, circleLocation.height/3);
But this example's not correct, the circle is not turn center. What is this formula? How can find correct center of cirle and how to convert Vector2?
Reading between the lines here, I wonder if issue has nothing to do with your code here, but with the rotation point of your steering wheel object, which isn't where you want it to be.
A common technique to change the rotation point of a game object is to make that object the child of another game object in the Unity Editor. Then set the position of the child object as you wish relative to the position of the parent object. Now, rotate the parent object. The child object will automatically rotate around the parent object's position, since it's transform (including position/rotation) is relative to it's parent's.
You seem to be defining your circle using a Rect so just use the Rect.center property:
circleLocation.center;