Search code examples
unity-game-engine2dgravitygameobjectrigid-bodies

ConstantForce and RigidBody


Im quite new to programming and working with the coding system in Unity. I have searched the web for this but I still haven't found a solution or proper answer.

Could ConstantForce affect a GameObject without a RigidBody?

I am wanting to create a magnetic like effect on my GameObject so that it is pushed backwards like a strong wind on an incline while I am trying to move forward. I tried to use a RigidBody but the gravity of the RigidBody makes the GameObject slide down the incline. Because of this effect I haven't started scripting the movement because I dont want the gameObject to start sliding backwards when forward motion stops. I want this to be done by the ConstantForce.

Thank you in advance for any answers and I hope I was able to explain this well.


Solution

  • You will need the RigidBody to be able to apply ConstantForce.

    Constant Force is a quick utility for adding constant forces to a Rigidbody. This works great for one shot objects like rockets, if you don’t want it to start with a large velocity but instead accelerate.

    You can disable the gravity on a RigidBody to solve your problem.

    if(collider.attachedRigidbody)
       collider.attachedRigidbody.useGravity = false;