- Home /
Unity rigidbody2D with Vector3
Hi, I'm trying to make repel function(2D platform). I found this code below in the sample project of unity2D
Vector3 hurtVector = transform.position - enemy.position + Vector3.up 5f; rigidbody2D.AddForce(hurtVector hurtForce);
So I just removed "+ Vector3.up * 5f;" part and thought it would work. But it didn't work as I expected. So I tested several Vector3 to see how things work.
Vector3(0, 1, 0) worked fined. My object moved up as expected. However, Vector3(1, 0, 0) and Vector3(0, 0, 1) didn't work as I expected. My object didn't move forward nor right, only slightly dragged along with ground.
Why would only y-axis work fine and x,z-axis not?
Where have I gone wrong? Is there any solution?
Which value is hurtVector when you use Vector3(1,0,0) and Vector3(0,1,0)?
hurtVector = new Vector3(1,0,0); or hurtVector = new Vector3(0,1,0); is what I did when testing.
Your code generate much more power that 1 (the distance + 5), try with Vector3(100, 0, 0).
Is this runnig inside a FixedUpdate?
It's in OnCollisionEnter2D. If I give really big x-value, like 10000, it just get dragged along ground short distance really fast(teleports). I think I'm getting something. When I make the object 'steps on' the repelling object, it works as expected. Can it be some friction with ground?
I would bet that it's because you're dragging along the ground, and since it's a force being added to the rigidbody, that force is being lossed in friction. Try doing Vector3(1, 1, 0)
Answer by Sisso · May 08, 2014 at 12:22 PM
I have half of the answer. 2d "don't have" the Z axis, so don't expect movement back and forward.
Your answer

Follow this Question
Related Questions
Question about Rays and why getPoint function 1 Answer
Linking array of sets of vector3's together 1 Answer
InverseTransformDirection and TransformDirection 1 Answer
why is my cube moving sideways in inconsistent distances? 2 Answers
nullifying targetObject after a Smooth LookAt Transition 2 Answers