Hi, I got an axe and i want to make it swing with the fixed point is the red circle in the pic. I’ve tried using the hinge joint 2D but it just rotates circularly (my aim is to swing between 2 angles), maybe i don’t know how to adjust the value properly. Can anyone give me some workarounds?
@midsideslimshady , i know this is an old post and all but I kinda ran into a similar problem`using
a 3d project but this is how I worked around it
UnityEngine;
public class Penduling : MonoBehaviour {
public float delta = 1.5f; // Amount to move left and right from the start point
public float speed = 2.0f;
public float direction = 1;
private Quaternion startPos;
void Start()
{
startPos = transform.rotation;
}
void Update()
{
Quaternion a = startPos;
a.x += direction * (delta * Mathf.Sin(Time.time * speed));
transform.rotation = a;
}
}
`
The algorithm works really well, great work JanZagar. Thank you!
P.S. I have problems with hinge joint too, it’s not worked like I want…
HingeJoint2D has angle limits. You can use those to prevent 360 rotation.
You could check jointAngle in Update() for when it reaches the limit and then AddTorque or change the direction of the motor.
You could animate it and enable the hinge joint when it hits something, giving it the illusion of swinging.