- Home /
How to make an Object follow a projectile motion pathway
Beginner Here. I was watching a tutorial on how to make a pathway projection using line renderer. And it worked. I can input a initial velocity and a angle and it renders the path it would follow. But now I want to have an object follow this pathway. How would I go on about this?
Pathway Projection Code
void RenderArc()
{
lr.positionCount = resolution + 1;
lr.SetPositions(CalculateArcArray());
}
//calculates the points needed to the arc
Vector3[] CalculateArcArray()
{
Vector3[] arcArray = new Vector3[resolution + 1];
radianAngle = Mathf.Deg2Rad * angle;
float maxDistance = (velocity * velocity * Mathf.Sin(2 * radianAngle)) / gravity;
for(int i = 0; i <= resolution; i++)
{
float t = (float)i / (float)resolution; //ensures all points are equally spaced
arcArray[i] = CalculateArcPoint(t, maxDistance);
}
return arcArray;
}
//calculate height and distance of each vertex
Vector3 CalculateArcPoint(float t, float maxDistance)
{
float x = t * maxDistance;
float y = x * Mathf.Tan(radianAngle) - ((gravity * x * x) / (2 * velocity * velocity * Mathf.Cos(radianAngle) * Mathf.Cos(radianAngle)));
return new Vector3(x, y);
}
I recently made a system where you have bullets that can ricochet off the walls, allowing sniping of enemies behind walls etc. A laser (line renderer) shows the trajectory a bullet would follow (which it does upon shooting). But it uses Raycasts and Reflect to calculate the trajectory. If you want to see how that worked I could tell, but it is quite different than the approach you have here.
$$anonymous$$ay I see the code? Am still quite new at Unity but I learn quite well by just reading and understanding other people's code.
It is a recursive function that runs depending on how many relfections you want
private void CalculateTrajectory(Vector3 position, Vector3 direction, int reflectionsRemaining) {
if (reflectionsRemaining <= 0) {
return;
}
Vector3 start = position;
Ray ray = new Ray(position, direction);
if (Physics.Raycast(ray, out RaycastHit hit, maxDistance)) {
direction = Vector3.Reflect(direction, hit.normal);
position = hit.point;
}
} else {
position += direction * maxDistance;
}
DrawLine(start, position);
CalculateTrajectory(position, direction, reflectionsRemaining - 1);
}
//Function to draw line based on calculated trajectories
private void DrawLine(Vector3 start, Vector3 end) {
GameObject myLine = new GameObject {
tag = currentTag
};
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.startWidth = 0.05f;
lr.endWidth = 0.05f;
lr.SetPosition(0, start);
lr.SetPosition(1, end);
}
Answer by tormentoarmagedoom · Jun 21, 2018 at 08:33 AM
Good day.
IF you know all the points (vertex) of the line redered, you only need to make the object go to the 1 by 1.
You can use the function Lerp to smooth the movement.
something like this:
object.transform.position = Vector3.Lerp(object.transform.position, NextVertex.transform.position, 0.2f)
(Take not i'm using t parameter as 0.2 to do a very very samooth movement, but of course you can try to increase it up to 1)
Bye!
Is there a way of using AddForce or Velocity? Because I want the object to be interrupted by other walls or other objects. So for example the ball initially follows the ball but a wall can block it. (I don't need the line renderer to show the path of the ball after bouncing). Thanks!
Your answer
