- Home /
[RESOLVED! :D][C#] Friction/Deceleration - Compiler won't let me damp individual Rigidbody axes to 0. Force in opposite direction maybe?
Trying to add friction/deceleration to my rigidbody (rb) movement code when no input is received from the player. I run into a problem at this part:
rb.velocity.x = Mathf.SmoothDamp(rb.velocity.x, 0, ref decelVelx, moveDecel);
rb.velocity.z = Mathf.SmoothDamp(rb.velocity.z, 0, ref decelVelz, moveDecel);
It won't compile because I can't set the velocity of the rigidbody axes directly. I've tried storing the velocity in a new Vector, but it doesn't do anything. I've also tried invoking a force in the opposite direction of the current velocity (I don't have the exact code from that iteration, but IIRC, it was something like this):
rb.AddRelativeForce(-rb.velocity.x,0,-rb.velocity.z);
but then my character moves in a circle whenever you let go of any movement buttons.
Any thoughts on how to fix this?
Edit: Getting the opposite force thing to work over a smoothed rate would actually work a lot better for my game, but I'll take either for now. Bonus points if you can help with both solutions ;)
When you say "I've tried storing the velocity in a new Vector, but it doesn't do anything", what code have you used ? Try:
Vector3 velocity = new Vector3(
$$anonymous$$athf.SmoothDamp(rb.velocity.x, 0, ref decelVelx, moveDecel),
rb.velocity.y,
$$anonymous$$athf.SmoothDamp(rb.velocity.z, 0, ref decelVelz, moveDecel)
);
rb.velocity = velocity ;
$$anonymous$$an, when I saw that you commented, I was sure you'd hit me with the right answer; but it's not working, sadly :( No point in even telling you what I've tried as far as storing in a new vector because your code is way more sound and it still isn't working for me! Not for naught, though, as your code example taught me something new that I never thought of: that I can store $$anonymous$$ath functions – and other functions that return a value – as a vector coord! A+ as always; this particular example just didn't work for me (it compiles fine, just doesn't decelerate my rigidbody character; it still feels like I'm running on ice).
Side note: this code supposedly works in js. It's just the fact that I can't implicitly set the values of the inidividual rigidbody.velocity axes in C# that's the issue.
Answer by Hellium · Nov 29, 2017 at 10:43 PM
After reading the doc, I learned that Mathf.SmoothDamp changes the value of the 3rd parameter. Give the following a try :
float velocityX = rb.velocity.x ;
float velocityZ = rb.velocity.z ;
Mathf.SmoothDamp(rb.position.x, 0, ref velocityX, moveDecel);
Mathf.SmoothDamp(rb.position.z, 0, ref velocityZ, moveDecel);
rb.velocity = new Vector3( velocityX, 0, velocityZ ) ;
Had to change rb.position (I was like "what the hell" when I kept reorienting to the origin when I stopped moving; it was pretty funny :p) to rb.velocity – which I'm sure was just a slip on your part – but it works!! Thanks again dude, you wanna just code this game for me? Haha if you wanna add your code in a comment, I'll accept it as the answer yet again! You're f***in awesome.
The first parameter of the $$anonymous$$athf.SmoothDamp seemed to require the position (according to the documentation). But if you get the desired result when passing rb.velocity, it's all good!
Glad I'm able to help you! Good luck for your game :)
Hmm very interesting. I thought the first parameter was the initial value, and the second was the desired/target value. Whatever works, eh? ( He says as he prays that this doesn't come back to bite him in the ass down the road )
Answer by Buckslice · Nov 29, 2017 at 10:26 PM
You can set a rigidbody velocity directly with a Vector3 like so
Vector3 v = new Vector3(1,0,0);
rb.velocity = v;
Heres a relevant thread as well
I personally would just do something like this
rb.drag = noInputs ? 2.0f : 0.0f;
or
rb.velocity = rb.velocity * 0.98f; // in FixedUpdate()
The only issue is that it would stop too abruptly rather than by a variable rate I can adjust if I did the first method (and I can't mess with drag based on what I plan to do with my game; it'll get really wonky, really fast). I've already tried something similar and then doing (using your example) something like:
Vector3 v = new Vector3(0,0,0);
v.x = $$anonymous$$athf.SmoothDamp(rb.velocity.x, 0, ref decelVelx, moveDecel);
v.z = $$anonymous$$athf.SmoothDamp(rb.velocity.z, 0, ref decelVelz, moveDecel);
rb.velocity = v;
..which compiles, but doesn't affect movement.
You can just do: rb.veloicty = new Vector3(rbvelocity.x -= deAccel, etc, etc);
No probs dude. I was learning it all a few months ago :P
Oh, and thank you for your input, Buckslice! Greatly appreciated! :) The issue has been resolved in the OP comment thread!
Your answer
Follow this Question
Related Questions
Strange/Buggy code behavier 1 Answer
My sphere loses velocity when jumping 2 Answers
Object shattering under a certain weight/force 3 Answers
relativeVelocity how to? (noob question) 2 Answers
Objects stop, but then pass through eachother, Rigidbody2D 0 Answers