- Home /
Rigidbody physics; forces doesnt move gameobject in a desired way
i have sphere with rigidbody component bordered by two cubes up and down the sphere that are fattened on x axis to seem like a wall. Now i am trying to make it move gradually increasing the velocity over time to the right (x axis)and the only inputs it takes is mousebutton press and when it happens it moves up or down (in opposite direction before input click) and when it touches the boundary walls it should again start to move at the same speed as before. the goal is to make it .smooth natural with including up and down movements when input is recieved. in order to achieve this i unchecked gravity so that it stays up or down and added a constant force in x axis. now whenever the input cilick happens this code runs rigidbody.AddForce(0,*speed*p,0 ,ForceMode.Impulse)
here p is 1 or -1 it flips the previous direction but after moving up or down it slows down and then again paces back which breaks the flow. I have tried several different ways. i tried to store the velocity just before the click and then add it with addforce (this stored velocity vector and impulse forcemode). i alsoft tried removing constant force component and move it at a constant speed in fixedupdate and various other methods, please take a look at the snaps of all the different codes that are attached with this question. None of them produce desired natural flow, they all slow down the ball after collision but only adds up the velocity gradually even when using add force with impulse. now i know there is a possibility to not use forces at all but i really want to know if this could be done with forces. PLEASE HELP!!!
Answer by Link17x · Mar 15, 2020 at 09:42 AM
You are calling a method every frame, which starts a coroutine every frame. Firstly, you should use FixedUpdate for physics rather than Update. Secondly, you don't want to be calling a coroutine in any kind of Update loop, because when you start a coroutine it will be repeating itself every 0.2 seconds. Every time you start a coroutine, it will start a new one rather than overwrite the current one.
Change this so you either use FixedUpdate or only one coroutine and you can use WaitForFixedSeconds. Then call your coroutine in Start or where ever (but make sure it isn't in an Update loop)
See how your physics behaves after that, then go from there.
Thank you for replying. i totally missed that perhaps because i was too tired by the time i came to this particular idea to try and improve for the desired outcome as i thought it might never produce effect with forces and planned to try translate or moveposition or other. although i will try this again and revert back on the progress if any. Thanks :)