0
\$\begingroup\$

I have this code and I'd like to add a shotgun to the game by slightly modifying the code. I've looked online for any example but everything I find is for FPS not a TDS.

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);
    }
}

I have a basic understanding of most of this code. I don't quite understand how shootRay.direction understands transform.forward means always the front of player. As DMGregory stated adding a variable of some sort that runs through a for loop firing multiple different angles (or simply different coordinates if making angle rays is over my head) is the route I first thought of but I cannot find a way to implement it. I'm having issues with understanding how to manipulate ray coordinates because they cannot be changed by floats which is the extent of my current knowledge of problem solving.

\$\endgroup\$
  • 1
    \$\begingroup\$ In order to give you the help you need it would be helpful to know how much of this code you understand. \$\endgroup\$ – Philipp Dec 30 '15 at 2:00
  • 1
    \$\begingroup\$ It seems all you'd need to do is add a random offset to shootRay.direction and fire multiple times with different offsets. How much of that do you know how to do already? \$\endgroup\$ – DMGregory Dec 30 '15 at 2:35
  • \$\begingroup\$ I think you use Unity. So, you can make a new object, add the script to it and change some properties. \$\endgroup\$ – H. Pauwelyn Dec 30 '15 at 14:15
  • \$\begingroup\$ @Philipp I have a basic understanding of most of it. I don't quite understand how shootRay.direction understands transform.forward means always the front of player. As DMGregory stated adding a variable of some sort that runs through a for loop firing multiple different angles (or simply different coordinates if making angle rays is over my head) is the route I first thought of but I cannot find a way to implement it. I'm having issues with understanding how to manipulate ray coordinates because they cannot be changed by floats which is the extent of my current knowledge of problem solving. \$\endgroup\$ – Gutsee Dec 30 '15 at 17:57
1
\$\begingroup\$

The "realistic" way to handle this is to just change the shootRay.direction vector.

for (var i = 0; i< pellets; i++){
    var offset = Random.insideUnitSphere * accuracy;
    shootRay.direction = transform.forward + offset;
    //then your raycast
}    

Random.insideUnitSphere will give you a random point inside a unit sphere.

accuracy should be a small float value to lessen the effect of insideUnitSphere

When transform.forward is added to the offset it will change they x, y and z coordinate for the direction

The easiest way is simply to reduce the damage based on the distance away the target is.

edit: Didn't notice what you said about examples for FPS. Why do feel like handling it different than a FPS?

Edit 2: You can set offset.y=0 to make sure there is no spread.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This code seems to be working except some of the shots are going above the enemies. I believe it is because the change in the y coordinates in Random.insideUnitSphere. The game is a TDS so there is no Y axis aiming. On a hunch I tried Random.insideUnitCircle, but realized that not only doesn't run, but that is x, y coords. Is there a way to change this? Thank you very much, you're a big help. \$\endgroup\$ – Gutsee Dec 30 '15 at 22:12
  • \$\begingroup\$ @user3167069 Just set shotRay.direction.y = 0 before raycasting. \$\endgroup\$ – DMGregory Dec 31 '15 at 13:49
  • \$\begingroup\$ @DMGregory that is assuming that shootRay.origin.y == 0. setting it to shootRay.origin.y ensures that it is at the same point in y. \$\endgroup\$ – Adam P Dec 31 '15 at 13:54
  • \$\begingroup\$ @AdamP No, that's incorrect. Ray.direction, as the name implies, is a direction, or relative offset. It is not a second point on the ray. So a ray with a direction.y = 0 travels along an x-z plane passing through whatever point origin describes. \$\endgroup\$ – DMGregory Dec 31 '15 at 14:04
  • \$\begingroup\$ You are correct. I am changing it back. \$\endgroup\$ – Adam P Dec 31 '15 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.