Vector value is (0.0, 0.0) when using variable
Hi,
It's the second time it happens to my game. When I use a Vector2(hSpeed,0) (or Vector3), it always return with 0 value. But when I give it a brute value it works fine. I use it like that :
rb2d.AddForce(new Vector3(hSpeed, 0, 0));
But the strange thing is that I use it the same way with transform.translate();
transform.Translate(new Vector3(hSpeed, 0, 0));
And it works fine ! But in a Debug.Log(); it displays (0.0, 0.0).
10 hours works on my game and you can just move, jump, if you jump on a monster it Destroy it and makes you bounces. 2 hours to do that, 8 hours to try to debug stupid Unity Behaviour like this... It's really pissing me off to always find a workaround with ducktape.
Can someone explain why it does this ?
Edit : I use Unity 5.6.3p1, which is the last version available.
What is the thing you debug exactly and where?(show the code and explain what is the expected value and why in your opinion it should be different) It is most likely your misunderstanding of what is the state of the object in the situation you are debugging it and logging the result which is (0.0, 0.0) at that moment.
Yes sorry I should be more precise. The debug and hspeed are :
hspeed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
Debug.Log(new Vector3(hspeed, 0, 0));
And it returns (0.0, 0.0, 0.0) when hSpeed is around 0.016f But it seems to returns correct value when used in Transform.Translate(new Vector3(hSpeed, 0, 0)); because it moves my character, but not with Rigidbody2D.AddForce(); And like I said when I do something like Rigidbody2D.AddForce(new Vector3(0.016f, 0, 0)); it works fine. And using Vector2 or Vector gave the same result.
After deeper research, I found that a lot of people are facing this issue from serverals Unity versions. Oldest post on it date from $$anonymous$$arch of this year.
When outputting a vector, the values are rounded, explaining why you have (0,0,0)
.
I think the reason why your object does not move using AddForce
, is that the force you apply is not big enough (compared to the mass of the rigidbody). Try to increase the applied force with a factor you can change in the inspector.
I don't think so, when I display hSpeed it's not rounded at all, maybe it's like that when outputing from a class contructor but I find the idea a bit stupid as it can leads you to search elsewhere for the issue, and it's not precised in Unity documentation. But if you're right, is there a way to force it to display compete value ? By the way, I'll try your solution this evening and give you a feedback about.
Your answer

Follow this Question
Related Questions
RayCast not working as expected, help! 1 Answer
Is there a Avatar Mask for 2D skeletal Animation? 0 Answers
Collider visibility in Scene View 0 Answers
How to I script a "aoe" click to change multiple sprites in one click,How do I make a 0 Answers
UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer