- Home /
Swinging Object
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?
Try it using transform component like :
public float angleRange;
void Update ()
{
transform.eulerAngles = new Vector3 (0, 0, $$anonymous$$athf.PingPong (Time.time * 10f, angleRange) - angleRange / 2);
}
@5c4r3cr0w my aim is the axe rotates around a fixed point it's the red point in the pic. Your code doesn't make the axe rotate around a fixed point, it's just a normal rotation
You need to create an empty object an by parenting it properly to your axe it'll work as you desire
You could rotate AXE from red point also with the code provided by @5c4r3cr0w (if currently its rotating at center) .. For that, Add your AXE game object into another gameobject such that, the pivot of the new gameObject (parent) comes at the red point of the AXE gameobject. Then rotate the new gameObject
Answer by TheDoubleG · May 26, 2017 at 04:17 PM
@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;
}
} `
Answer by Develoop · Nov 06, 2020 at 11:34 PM
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...
Answer by idbrii · Feb 23, 2021 at 05:10 PM
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.
Answer by flare38888 · Feb 23, 2021 at 06:24 PM
You could animate it and enable the hinge joint when it hits something, giving it the illusion of swinging.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091333im_/https://answers.unity.com/themes/thub/images/avi.jpg)