Wednesday, September 4, 2013

Update 16: Alpha 1.4: Throwing

In the last few Alpha releases, I've been focusing on stabilizing the camera and shooting mechanics. I feel much better about how it all looks, especially the camera movement, with all bugs around the camera now addressed.

Although I have a few timing issues with the shooting still to solve, I've moved on to the next type of attack, throwing grenades. I started by working through the physics and showing the trajectory. 

With the help of two very important links:

  1. Some excellent theory from the Castle Story: http://www.sauropodstudio.com/dev-diary-number-eight/
  2. A script to draw the line from Unity3D wiki: http://wiki.unity3d.com/index.php/Trajectory_Simulation

Using this, I was able to calculate the distance to the target, based on a fixed angle of 45 degrees: this meant that I can throw my grenade in a nice parabolic curve towards the target. 

Here are two screen shots showing this in action in my test level, with the red line replaced by a trail of smoke: 

Just before the grenade hits...


Just after the grenade hits...


When my grenade collides against my target, it 'explodes'. At the moment it's just an expanding red sphere - particle effects to come later. I use the Physics.OverlapSphere() function to look for items within a certain radius:

Collider[] hits = Physics.OverlapSphere(myProjectile.transform.position, explosionRadius);


If any of my collider hits are a player, I deal damage, but if it's destructible terrain, I use the AddExplosionForce() function like this:

//If destructible terrain was hit, destroy it
if (hit.collider.tag == "DestructibleTerrain") 
{
    //Add Rigidbody to terrain/wall
    hit.transform.gameObject.AddComponent<Rigidbody>();
    
    //Add explosive force at the point of the projectile impact
    hit.transform.rigidbody.AddExplosionForce(explosionForce, myProjectile.transform.position, explosionRadius);
}

This has two effects: it removes the destructible terrain from the explosion area, and it propels the same terrain across the map, which I think is pretty cool...

You can download version Alpha 1.4 here: http://www.samsmithnz.com/content/tbe/tbe_alpha1.4.zip.

1 comment:

  1. Great blog. All posts have something to learn. Your work is very good and I appreciate you and hopping for some more informative posts. Alpha php

    ReplyDelete