New to Unity/C#, I've got a few questions on a tutorial.
I'm doing the tutorial on Survival Shooter. I'm following the videos and the PDF that has changes for Unity 5. In part 7 there are some pre-programmed scripts. The Shoot() script is below, which is when the player shoots the gun. I have a couple questions if anyone can please answer for me.
-
Why is the layermask, which is shootableMask, stored as an integer, and what do the arguments in the Physics.Raycast method actually mean? Raycasting in general is just confusing for me.
-
You will notice that right after gunLine which is the Line Renderer, is enabled, its position is set to (0, transform.position). But towards the bottom, the code reads: gunLine.SetPosition (1, shootHit.point); The shootHit.point I can tell basically means where the bullets are actually going to land, but I don't understand where the 1 came from. In the tutorial it was described as the "second point" but that just confused me more.
-
After the raycasting is done, there is this line of code: EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
shootHit was declared as a RaycastHit. No idea how this is a reference to the script EnemyHealth, which is also in the game. If someone could break down what's happening in this line of code, it would be great.
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
Any help would be greatly appreciated.
Posted Jul 31, 2015, 11:58 PM UTC