- Home /
Question by
unity_9r94SetIXbqtig · Oct 06, 2018 at 12:27 PM ·
rotationvelocityphysics2d
Rotate around a position using Physics in 2D
Hi ! I want to rotate an object around another using physics in 2D (I want to keep the velocity so the object can throw himself like Spiderman !). The problem is that it goes crazy when I try to use the cross product and I don't really know why. Can someone explain to me why it's behaving like that ? Thanks !
Code and stuff :
capture.png
(4.4 kB)
gifprog.gif
(103.6 kB)
Comment
Answer by LCStark · Oct 06, 2018 at 01:49 PM
Have you tried checking the values you get?
Vector3 dir = (target.transform.position - transform.position).normalized;
cross = Vector3.Cross(dir, Vector3.forward);
Debug.DrawRay(transform.position, cross * speed);
rb2d.AddForce(cross * speed);
I'd guess you have to add some force to keep your object in close range. Basicly, you should add the force of your rope pulling your object towards the hook point. The farther away from it you move the stronger the force, depending on the flexibility of your rope.
Answer by unity_9r94SetIXbqtig · Oct 06, 2018 at 02:36 PM
Hi, thanks for the answer it inspired me ! I found out that the x value of the velocity wasn't 0 when it should have been. So here's my changes :
//Wrong way
rb2d.AddForce(cross * speed);
//"Correct" way
rb2d.velocity = cross * speed;