So, I’ve went through this series of tutorials:
The player shooting script:
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public static int damagePerShot = 20;
public static float timeBetweenBullets = 1f;
public static float range = 100f;
float timer;
Ray shootRay; //see what we hit
RaycastHit shootHit; //return what we hit
int shootableMask; //we can only hit shootable things
ParticleSystem gunParticles; //ref
LineRenderer gunLine; //ref
AudioSource gunAudio; //ref
Light gunLight; //ref
float effectsDisplayTime = 0.2f; //how long are effects gonna be viewable
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable"); //shootable layer
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}
if (timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects (); //turn back off
}
}
//ref from another script
public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f; //reset timer; wait till next shot
gunAudio.Play ();
gunLight.enabled = true; //show light
gunParticles.Stop (); //if they are still playing, stop them
gunParticles.Play (); //start playing (sync all the components)
gunLine.enabled = true; //enable the renderer
gunLine.SetPosition (0, transform.position); //from 0 (begining of the barrel) to position
shootRay.origin = transform.position; //start at the tip of the gun
shootRay.direction = transform.forward; //z-axis (forward)
//shoot at ray, if we hit something return the end of the line where its hit, if not, draw a long line
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask)) //shootray = that way; shoothit = what we hit; range = 100f; shootableMask = shoot things which are able to be shot
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> (); //if we hit something, get its script
if(enemyHealth != null) //check if we hit nothing
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point); //line from begining to what we hit
}
else //if we dont hit
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range); //draw a line to end
}
}
}
Been making my own game for a month after that, added lots of stuff etc… The only thing I have left now that I’m not happy with is the shooting mechanism. Instead of the rifle straight line, I’ve added a laser with the same script and object as in the tutorial. It does locate the mouse location and shoots where it’s supposed to if my character is facing down, otherwise the laser is too much to the right/left.
My guess is that I need to reposition my Players child object “GunBarrelEnd” (from the tutorial), but I cannot seem to get it right no matter what I do. So, does anyone have any suggestions about positioning this game object? Or any other approach to this problem?
While I’m here, I would like to add a few spells also… should I follow the same shooting script in the tutorial and only add a different particle effect (given that I can somehow slowdown the RayCast - How?) or is there a different approach for spells?