- Home /
Simulating trajectory
Hello, I have tank turret with cannon, when I fire it initialize shell prefab and sets velocity etc. shell has this physics material settings and I want to simulate bouncing too. https://imgur.com/a/9fW2HPi I dont know how to use physics material settings in this code, for boucing Here is the code
public Rigidbody projectilePrefab;
public Transform projectileOrigin;
public LineRenderer lr;
public int segmentCount = 20;
// Length scale for each segment
public float segmentScale = 30;
private Collider _hitObject;
public Collider hitObject { get { return _hitObject; } }
static private Rigidbody projectile;
private void DrawTrajectory()
{
Debug.Log($"{projectile.velocity.magnitude}");
Vector3[] segments = new Vector3[segmentCount];
segments[0] = projectileOrigin.position;
// The initial velocity
Vector3 segVelocity = projectile.velocity;
for (int i = 1; i < segmentCount; i++)
{
// Time it takes to traverse one segment of length segScale (careful if velocity is zero)
float segTime = (segVelocity.sqrMagnitude != 0) ? segmentScale / segVelocity.magnitude : 0;
// Add velocity from gravity for this segment's timestep
segVelocity = segVelocity + Physics.gravity * segTime;
// Check to see if we're going to hit a physics object
RaycastHit hit;
if (Physics.Raycast(segments[i - 1], segVelocity, out hit, segmentScale))
{
// remember who we hit
_hitObject = hit.collider;
// set next position to the position where we hit the physics object
segments[i] = segments[i - 1] + segVelocity.normalized * hit.distance;
// correct ending velocity, since we didn't actually travel an entire segment
segVelocity = segVelocity - Physics.gravity * (segmentScale - hit.distance) / segVelocity.magnitude;
// flip the velocity to simulate a bounce
segVelocity = Vector3.Reflect(segVelocity, hit.normal);
/*
* Here you could check if the object hit by the Raycast had some property - was
* sticky, would cause the ball to explode, or was another ball in the air for
* instance. You could then end the simulation by setting all further points to
* this last point and then breaking this for loop.
*/
}
// If our raycast hit no objects, then set the next position to the last one plus v*t
else
{
segments[i] = segments[i - 1] + segVelocity * segTime;
}
}
// At the end, apply our simulations to the LineRenderer
// Set the colour of our path to the colour of the next ball
Color startColor = Color.red;
Color endColor = Color.red;
startColor.a = 1;
endColor.a = 1;
lr.SetColors(startColor, endColor);
lr.positionCount = segmentCount;
for (int i = 0; i < segmentCount; i++)
{
lr.SetPosition(i, segments[i]);
}
}
sooo, what kind of bouncing are you looking for? Physics materials are easy to work with, and have a bounciness limit, or are you hoping for a cartoonish reflection as in the old wii tank game?
At line 50, I want to reflect based on physics material settings, I dont know how
Your answer
Follow this Question
Related Questions
Simulating a trajectory that takes collisions into account 2 Answers
how to draw trajectory of rocket for faux gravity in unity???? 0 Answers
Not Able to Convert 2D trajectory(right Side) into 3dTrajectory(forward Direction)... 1 Answer
Create a 3D trail renderer like Hawk eye simulation 1 Answer
Problem with a ballistic trajectory simulation (rigidbody.velocity) 0 Answers