Line Renderer for golf game
So I'm trying to put together a simple golf game and I want to use the line renderer component to show the arc the ball will travel in (or even just a simple point to point showing where it should end up based on the power). The problem is there really aren't any great resources showcasing the use of it in Unity 5, which is what I am using for this game.
I checked out the unity manual for line renderer (located here: http://docs.unity3d.com/Manual/class-LineRenderer.html) and have done a search to find something to show me about how it works.
Does anyone have any suggestions on good tutorials or resources that I can use to learn more about this?
Answer by SarperS · Jun 23, 2016 at 04:46 AM
It is called "trajectory prediction" and there are tons of material regarding this matter. Here is one;
http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html
Answer by MasterLG · Jun 23, 2016 at 04:51 AM
I couldn't think of any resources, and I mostly just screw with things until they work.
Here's a small sample script I whipped up just then called Parabolizer. In a new scene with the default objects, just attach a LineRenderer component to another object and set the material to something you can make sense of, and then attach this script to the same object that has the LineRenderer.
using UnityEngine;
using System.Collections.Generic;
public class Parabolizer : MonoBehaviour {
void Start() {
// just some set up parameters
float minY = -5f;
Vector3 launchVelocity = new Vector3(Random.value * 20f, Random.value * 5f, 0f);
List<Vector3> points = new List<Vector3>();
// gather a list of points along a parabola - not the most elegant way to do things
Vector3 current = new Vector3(-5f, 0f, 0f);
while (current.y >= minY) {
points.Add(current);
current += launchVelocity * Time.fixedDeltaTime;
launchVelocity += Physics.gravity * Time.fixedDeltaTime;
}
// getting the line renderer on this object
LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer>();
// fiddling with the width
lineRenderer.SetWidth(0.5f, 0.05f);
// update how many vertices are to be expected
lineRenderer.SetVertexCount(points.Count);
// setting all positions in one go
// lineRenderer.SetPositions(points.ToArray());
// setting points individually instead
for (int p = 0; p < points.Count; p++) {
lineRenderer.SetPosition(p, points[p]);
}
}
}
I hope this helps as a starting point. Is there anything else you'd like to know?
Your answer
Follow this Question
Related Questions
Line Renderer issue 0 Answers
Making a Line Renderer only visible to one Camera 1 Answer
How to use the results of a dice roll? 2 Answers
How do i Instantiate sub-Points with in Multiple Points?? 0 Answers
OnGUI will not show up? 1 Answer