- Home /
Can't seem to use AddForce
Just trying to use AddForce to add an upwards force to an instantiated sphere. Must be something I'm leaving out or not coding properly. Here's my code. Note: touchLength is a variable which will have a value between 1 and 250, usually closer to 250.
void Update ()
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
//create sphere
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//add a rigidbody
Rigidbody spheresRigidBody = sphere.AddComponent<Rigidbody>();
//add mass
spheresRigidBody.mass = 5;
//fling sphere
sphere.rigidbody.AddForce(new Vector3(0, touchLength, 0));
}
}
}
What are you seeing in unity as a result from this script?
A.k.a what is going differently than you want it to?
The ball is created and then simply drops from gravity. No upwards force is being applied to the sphere at all, not even when touchLength == 250.
You can check your drag,is$$anonymous$$inematic and static type because all these thing are related to rigidbody.
Drag and is$$anonymous$$inematic are not set anywhere, so I would guess they are at their defaults. I'm pretty sure is$$anonymous$$inematic is off as the sphere falls because of gravity. I'm not sure what static type is or how to check that.
Answer by Wisearn · Oct 31, 2014 at 01:23 PM
You just need more force, try
sphere.rigidbody.AddForce(new Vector3(0f, touchLength*50f, 0f));
5 in mass is INSANE, even with 1 in mass you need touchLength*10f to even see any change
so you can also change if you want
spheresRigidBody.mass = 1f;
or alter the touchLength*50f to higher value than 50f until it's good
You can accept it as an answer on the button below the downvote!
and np
Regular thing to try when things arent working as you think it should is to try using extremes (extremely high or extremely low or even negative) for the values.
Harshad$$anonymous$$'s answer here is also quite relevant, that you should try different forcemodes and different ways of applying force, even setting the velocity of the item directly might be more suitable for certain circumstances.
Answer by HarshadK · Oct 31, 2014 at 01:31 PM
The reason behind your object not being pushed upwards when the force applied is because your AddForce adds regular force (ForceMode.Force) that too in one frame only. This force applied is not enough to push your object upwards since in ForceMode.Force the mass also comes into picture and mass of your object is 5 which makes your applied force insufficient to push your object upwards.
Just to test this add a force with ForceMode.Acceleration which ignores the mass and you'll find that your object is actually pushed upwards a little bit. Or you can even reduce the mass of your object to test this like setting mass to 1.
If you want to throw your object based on some value where you apply force just once (like you are currently doing) you can apply a ForceMode.Impulse. Mind that applying a force equal to 255 with impulse will cause your object just to go way too high. You can actually adjust the value of force as per your need to apply to ForceMode.Impulse.