- Home /
AddForce to an object
I want the player to be able to throw an object (rigidbody). The code below instantiates the object at player/camera position, but then doesn't "throw" it - not sure what I'm doing wrong. Thanks/
private float forceLevel = 500f;
[SerializeField]
private GameObject throwingSphere;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(throwingSphere, transform.position, Quaternion.identity);
var rigBdy = throwingSphere.GetComponent<Rigidbody>();
}
}
$$anonymous$$ake sure the Rigidbody is not $$anonymous$$inematic.
$$anonymous$$ake sure the value of forceLevel is high enough.
$$anonymous$$ake sure Rigidbody's drag isn't too high.
Check the Rigidbody's constraint settings.
Try without the raycast. Use any vector that's not (0,0,0).
Use the correct Force$$anonymous$$ode (check the documentation).
If it still doesn't help, try setting the velocity directly ins$$anonymous$$d of using AddForce.
Answer by Magso · Jan 07, 2020 at 02:05 PM
The sphere being thrown isn't being referenced, instantiate the new sphere as a variable.
var newSphere = Instantiate(throwingSphere, transform.position, Quaternion.identity);
var rigBdy = newSphere.GetComponent<Rigidbody>();
Your answer
Follow this Question
Related Questions
Grab rigidbody 0 Answers
Raycast hit in unity 3d 1 Answer
Line Renderering Issue 0 Answers
is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers
How do I instantiate and shoot a ball along a ray cast? [ANSWERED] 1 Answer