Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fierce Waffle · Mar 21, 2011 at 09:47 PM · raycastdirectionclimbmuzzleflash

MachineGun script applied damage to the object that is not hit.

Here is my script that I have modified from the FPS Tutorial. I have made it so that it has a random spread rather than dead on accurate fire.

function FireOneShot (shotOwner : boolean, hitPoint : Vector3, hitNormal : Vector3) { var direction = SprayDirection(); var hit : RaycastHit; print("shot"); // Did we hit anything? //Invoke("muzzleClimb", 0.3); if(shotSpread <= spreadMax) shotSpread += spreadClimbAmount; if (Physics.Raycast (transform.position, direction, hit, range)) { var settingsArray = new String[2]; settingsArray[0]=damage+""; settingsArray[1]=localPlayerName;

         hit.collider.SendMessage("ApplyDamage", settingsArray, SendMessageOptions.DontRequireReceiver); 
     // Apply a force to the rigidbody we hit
     if (hit.rigidbody)
         hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

     var go : GameObject;

     var delta : float = -0.02;
     var hitUpDir : Vector3 = hit.normal;
     hitPoint = hit.point + hit.normal * delta;

     //what type of object did we hit?
     switch(hit.collider.gameObject.tag)
     {
         case "wood":
             go = Network.Instantiate(woodParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "metal":
             go = Network.Instantiate(metalParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "concrete":
             go = Network.Instantiate(concreteParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "dirt":
             go = Network.Instantiate(sandParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "explosive":
             var expObject : explosiveObject = hit.collider.gameObject.GetComponent(explosiveObject);
             expObject.hitTimeCount +=1;
             go = Network.Instantiate(metalParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "sand":
             go = Network.Instantiate(sandParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "water":
             go = Network.Instantiate(waterParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         case "acid":
             go = Network.Instantiate(acidParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
             break;
         default:
             return;
     }
     // Send a damage message to the hit object          
     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }
 bulletsLeft--;

 // Register that we shot this frame,
 // so that the LateUpdate function enabled the muzzleflash renderer for one frame
 m_LastFrameShot = Time.frameCount;
 enabled = true;

 // Reload gun in reload Time        
 if (bulletsLeft == 0)
     Reload();           

}

and here is the function for making the direction random. l

function SprayDirection() {
var vx = (1 - 2 * Random.value) * shotSpread;
var vy = (1 - 2 * Random.value) * shotSpread;
var vz = 1.0;
return transform.TransformDirection(Vector3(vx,vy,vz));
}

The problem I am having is that on this line

 hit.collider.SendMessage("ApplyDamage", settingsArray, SendMessageOptions.DontRequireReceiver); 

It still applies the damage to the item in the immediate forward direction rather than the random area. help is appreciated :)

What is not working about this script is lets say I looked straight on to another player and I shot. You could see sparks instantiated in the correct random area, but instead of damage being applied to that object, it was applied to the player because he was in the immediate forward direction of me.

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael 12 · Mar 22, 2011 at 01:28 AM 0
Share

Sorry I can't help with your question I'm new to code so this stuff baffles me buggy sometimes. But this could be EXACTLY what I need as I'm trying to do the same thing for my FPS game. But where do you put your particle systems for a setup like this? Are they all children of your individual weapons?

avatar image Fierce Waffle · Mar 22, 2011 at 01:51 AM 0
Share

Add me on AI$$anonymous$$ and I can help you lol. BTW this isnt the entire script, so certain things might not work

avatar image Fierce Waffle · Mar 22, 2011 at 01:53 AM 0
Share

my username is fiercewaffle

avatar image Michael 12 · Mar 22, 2011 at 03:06 PM 0
Share

I added you I think, my username is $$anonymous$$ichael $$anonymous$$night As long as I did that correctly, I've never had an AI$$anonymous$$ account before ;)

avatar image flaviusxvii · Mar 22, 2011 at 06:30 PM 0
Share

Depending on the spread you'd still expect some bullets to hit directly ahead right? Isn't that kind of the point?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fierce Waffle · Mar 27, 2011 at 11:35 AM

Solved. I dont know how I fixed it lol.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Raycast Decals,Sound and MuzzleFlash not working too well 0 Answers

How to restrict Direction? 0 Answers

Why i can't see my questions ? 2 Answers

Skyboxes, GetPixel and Raycast Directions 1 Answer

RayCast to all directions 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges