Velocity

v = delta d (distance) / delta t (time)

In Unity, when we move an object, we always adjust one or more of its x, y, or z values. 

In other words, we add a Vector3 to the position of the object.

Next, we are going to use starter project S0303. Start a new 3D project and add the starter package.

There are two tanks in the scene. But first, we will not use this scene.

Select File - New Scene and select 3D scene.

We are going to add a bullet to the scene. Go to Prefabs and find the Shell prefab in the folder.

Drag the Shell into the scene. You should be able to see it in the Game View.

If you cannot see it in the Game View, select the Main Camera and select Game Object and select 

Align with View. Now you will be able to see what this scene view is using.

We can select the Shell and move it closer to the camera.

Z axis is also known as its "forward axis". We want to move the bullet along its forward axis.

In the Scene View, we can select Local or Global space. Z is forward axis in the Local Space.

Remove some of the components in the inspector window. Remove: RigidBody, CapsuleCollider, 

Shell and DestroyShell scripts.

Add a new script called MoveShell and attach it to the bullet. The bullet must move across the screen

one meter per second.

In the Update()

this.transform.Translate(0, 0, Time.deltaTime * speed);

Define speed variable above

float speed = 1.0f;

Save and test.

Challenge: Move the bullet half the speed on the diagonal compared to forward axis.

Solution: Modify the Y axis.

this.transform.Translate(0, Time.deltaTime * speed * 0.5f, Time.deltaTime * speed);

Save and test.

Next, we will see how we can predict where an object will be in the future using 

velocity. This is often used in AI systems.

Predicting Future Locations of Game Objects

Create a new prefab from the Shell.Drag and drop the Shell Game Object from the 

Hierarchy Window to the Assets folder. Shell Prefab is created. 

If Unity asks if you want to keep the original prefab or create a variant, select

variant. Because, we changed the original prefab and we want to use this new variant.

you can give a new name for this prefab. We will use this 

prefab later to create bullets in front of the tank programatically.

After creating the new prefab, open the Scene with the tanks. 

Create a new C# script named FireShell. We need to instantiate a Game Object from

the modified Shell prefab. we want to position the Shell game object in front of the 

turret of the tank. Select the Tank in the Hierarchy Window and right click: select

create empty game object. Rename the new empty game object as Turret. We need to move its

position. Select Turret in the Hierarchy Window. Then select its position in the Scene view

and position it in front of the turret of the Tank.

Change the MoveShell script. Set y axis translation back to 0.

Now we can add fire.

Open FireShell script. Add public GameObjects for bullet and turret.

public GameObject bullet;

public GameObject turret;

Instantiate bullet in the Start function.

Instantiate(bullet, turret.transform.position, turret.transform.rotation);

Save. Attach FireShell script to the tank (green tank). Red tank is enemy.

Set bullet and turret. Bullet is a prefab. Set bullet using the Shell prefab.

Set turret using the Turret game object under tank in the hierarchy window.

Save and test.

Currently, enemy tank is facing green tank. Change its rotation. 

Turn the enemy (red) tank towards y axis up. Select the red tank first and then rotate it

towards y axis up direction. Open its Drive script. This code allows you to drive the tank.

We want to automate the movement and disable drive. In the Update function, 

copy transform.Translate(0, 0, translation) and paste one line below.

Comment the original line: //transform.Translate(0, 0, translation) 

and change the pasted line as follows:

transform.Translate(0, 0, speed * Time.deltaTime);

Also comment the transform.Rotate line below: //transform.Rotate..

Save and test it. Green tank fires a bullet and red tank is moving away.

Change the speed of the enemy tank to 1.0f;

Open FireShell script. Add some code to fire bullet when space bar is hit.

In the Update():

if (Input.GetKeyDown(KeyCode.Space))

{

CreateBullet();

}

Change the Start function to CreateBullet()

We need to determine the angle that the tank should be facing before it creates the bullet.

Add a new function call to Update()

if (Input.GetKeyDown(KeyCode.Space))

{

CalculateTrajectory();

CreateBullet();

}

Define the new function:

void CalculateTrajectory()

{

}

We need to calculate the time to impact. We need to solve a quadratic equation: p + vt = st

Bullet speed (s): From the tank to the impact point (Tankın mevcut konumundan çarpışmanın 

(mermi ile düşman tankın çarpışması) gerçekleşeceği konuma doğru tanımlanan vektör)

Target position (p) and target velocity (v): From enemy tank to the impact point (Düşman tankından

çarpışmanın gerçekleşeceği konuma doğru tanımlanan vektör ve hız)

When they (bullet and enemy tank) will be at the same time at the impact point? 

All of this depend on time (t): the time to impact. (Eğer t=3 ise 3 saniye sonra çarpışma gerçekleşir).

p + v.t = s.t

(p + vt)^2 = (st)^2

p.p + 2(p.v)t + (v.v)t^2 = s^2t^2

p.p + 2(p.v)t + (v.v - s^2)t^2 = 0

Quadratic equation format: ax^2 + bx + c = 0

When we solve this equation, x = -b +/- (b^2-4ac)/2a

t^2 and x^2 are similar. 

a is (v.v - s^2)

b is 2(p.v)

c is p.p

We can find t in the equation.

Change the return type of the function

Vector3 CalculateTrajectory()

{

}

Modify Update function

if (Input.GetKeyDown(KeyCode.Space))

{

this.transform.forward = CalculateTrajectory();

CreateBullet();

}

Implement the function. Remember to add the Enemy tank game object.

public GameObject enemy;

Set its value from the inspector window.

Vector3 CalculateTrajectory()

{

//Calculations must be relative to the shooter. Subtract the shooter's position.

Vector3 p = enemy.tranform.position - this.transform.position;

Vector3 v = enemy.transform.forward * enemy.GetComponent<Drive>().speed;

float s = bullet.GetComponent<MoveShell>().speed;

//Drive ve MoveShell script isimleri. Eğer başka isim verdiyseniz buna göre değiştirin.

float a = Vector3.Dot(v, v) - s * s;

float b = Vector3.Dot(p, v);

float c = Vector3.Dot(p, p);

float d = b * b - a * c; (Try also -4ac)

if (d < 0.1f) return Vector3.zero;

float sqrt = Mathf.Sqrt(d);

float t1 = (-b - sqrt)/c;

float t2 = (-b + sqrt)/c;

float t = 0;

if (t1 < 0 && t2 < 0) t = 0;

else if (t1 < 0) t = t2;

else if (t2 < 0) t = t1;

else t = Mathf.Max(new float[] {t1, t2});

return t * p + v;

}

Change the speed of bullet and enemy to 3.

Save and test.

Press play and shoot lots of bullets. You will see that they will arc across the screen and as the 

tank moves, it will move through all of the bullets until a point where we cannot calculate a trajectory.

Modify the Update function to fix the problem.

if (Input.GetKeyDown(KeyCode.Space))

{

Vector3 aimAt = CalculateTrajectory();

if (aimAt != Vector3.zero)

[

this.transform.forward = aimAt;

CreateBullet();

}

}

Also modify CalculateTrajectory function

..

float t = 0;

if (t1 < 0 && t2 < 0) return Vector3.zero;

else if (t1 < 0) t = t2;

..

StatusPrototype
PlatformsHTML5
Authorfulminare
GenreShooter
Made withUnity

Leave a comment

Log in with itch.io to leave a comment.