- Home /
How to throw a rigidbody along line renderer?
I'm trying to write a realistic grenade throw. I'm using the below code to draw a line renderer arc between my player and and a point that follows the mouse.
void Update(){
var pointList = new List<Vector3>();
float vertecCount = 12;
for(float ratio = 0; ratio <=1; ratio += 1 / vertecCount){
var tangent1 = Vector3.Lerp(transform.position, point2.position, ratio);
var tangent2 = Vector3.Lerp(point2.position, grenadeTarget.transform.position, ratio);
var curve = Vector3.Lerp(tangent1, tangent2, ratio);
pointList.Add(curve);
}
grenadeArc.positionCount = pointList.Count;
grenadeArc.SetPositions(pointList.ToArray());
}
How can I instantiate a sphere and throw it along the line renderer using AddForce? I need to use the rigidbody so the grenade can bounce realistically.
Answer by Pangamini · Apr 14 at 12:46 PM
Probably not "along", because a rigidbody's trajectory would be a parabola instead of straight line (if there's no drag / velocity damping). So in order to throw a grenade to a specific point in world, you need to find this parabola and use its derivative to get a starting velocity. Ofc there are infinitely many parabolas, so you could either fix the throw speed or the angle.
I'd suggest that you project everything on a 2D plane (to simplify the math) that contains the throw origin, the destination point, and it's vertical. Your starting point (from where you throw) will be at point [0;0]. Then, use your math skills to find the parabola that crosses the origin and destination points.
Your answer
Follow this Question
Related Questions
Instantiate objects in an arc shape 1 Answer
Trouble calculating Arc offset position 1 Answer
Arc Movement Lerp, Mathf.Sin and Mathf.Pingpong question 1 Answer
Detect colliders in an arc 1 Answer
Gravity & Framerate 2 Answers