i am creating slingshot based ball game. Is any slingshot tutorial is available or how can i get a elastic physics in slingshot.
Thanks
i am creating slingshot based ball game. Is any slingshot tutorial is available or how can i get a elastic physics in slingshot.
Thanks
When you pull the rubberband a X distance from the rest point (where the rubberband isn’t stretched) you get a potential energy = Mathf.Pow(X, 2) * k /2. When you release the rubberband, the potential energy is transfered to the stone as kinetic energy = Mathf.Pow(v, 2) * m/2. Doing some simple math, we get the following equation:
v = X * Mathf.Sqrt(k / m);
Where v is the stone velocity (magnitude), X is the lenght the rubberband was stretched, m is the stone mass and k is the rubberband elastic constant.
In practical terms, you can do the following:
Let k as a public var, so you can adjust it in the Inspector;
X will be the distance the player pulled the rubberband from the rest position;
Create the stone at the rest position:
var stone:Transform = Instantiate(stonePrefab, rest.position, rest.rotation)
m is stone.rigidbody.mass; if you intend to use a different value from the prefab mass, set stone.rigidbody.mass to the m value you will use now;
Calculate the velocity v with the equation we derived above;
Get the direction vector. You can use the rest position minus the position where the player released the rubberband; this will result in a Vector3 we will call direction;
Set the stone velocity:
stone.rigidbody.velocity = direction.normalized * v;
The rest is ballistic theory, but you don’t need to care about this because physics will do the dirty job for you: the stone will fly following an arc under physics control.
you’re overthinking it. just get an angle, determine the ‘power’ and let it go. poof.
thanks aldonaletto and testure
@aldonaletto Thanks, even it helped me a lot. Can you please tell me how can we draw trajectory(path) of the projectile before shooting?