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)
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//add a rigidbody
Rigidbody spheresRigidBody = sphere.AddComponent<Rigidbody>();
spheresRigidBody.mass = 5;
sphere.rigidbody.AddForce(new Vector3(0, touchLength, 0));
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
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.