Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 EliteHedgehog56 · Apr 16, 2018 at 10:39 AM · rigidbodyraycastshootingbullets

change to raycast shooting instead of rigidbody shooting

I have two scripts; on is a shooting script and the other is bullet damage. The shooting script at the moment shoots rigidbody bullets. Is there a way I can change the shooting script to shoot raycast bullets instead of rigidbody bullets and then change the damage script to accommodate the raycast bullets?

shooting script:

 RequireComponent(AudioSource);
 var audioSource: AudioSource;
  var Projectile : Rigidbody;
  var ProjectileSpeed : int = 10;
  var FireRate : float = 10;  // The number of bullets fired per second
  var lastfired : float;      // The value of Time.time at the last firing moment
  var gunshot : AudioClip;
  var muzzleflash :Transform;
 
 
 
  function Start() {
     audioSource = GetComponent.<AudioSource>();
 }
 
  function Update ()
  {
 
        if (Input.GetAxis("Fire1")>0f)
      {
          if (Time.time - lastfired > 1 / FireRate)
          {
              lastfired = Time.time;
              var clone : Rigidbody;
              clone = Instantiate(Projectile, transform.position, transform.rotation);
              clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
              audioSource.PlayOneShot(gunshot, 0.7F);
              Instantiate (muzzleflash,transform.position,transform.rotation);
          }
      }
 
       
           
      }


bullet damage script:

 var dmg:float = 20;
  
  function OnTriggerEnter(hit:Collider){
  if(hit.tag == "Player"){
  hit.transform.SendMessage("Damage",dmg);
  }
  
  }
Comment
Add comment
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

1 Reply

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

Answer by shadowpuppet · Apr 16, 2018 at 03:32 PM

you can do away with the bulletDamage script if you use rayCast. This is C#. I don't know JS but should be close enough to adapt to Java

     Ray bulletRay;
             RaycastHit rayHit;
             public int dist = 45; //distance ray/bullet travel
     public float lastfired;
 Update () {
     if (Input.GetAxis("Fire1")>0f)
           {
               if (Time.time - lastfired > 1 / FireRate)
               {
                   lastfired = Time.time;
                 bulletRay = new Ray(transform.position, transform.forward*dist);
                 Debug.DrawRay (transform.position, transform.forward*dist, Color.yellow);
                 if (Physics.Raycast(transform.position, transform.forward, out rayHit,dist))
                 {
     
                     if(rayHit.transform.gameObject.tag == ("Player")) 
                         hit.transform.SendMessage("Damage",dmg);
             }
         }
         }
     
     
 
Comment
Add comment · Show 3 · 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
avatar image EliteHedgehog56 · Apr 16, 2018 at 09:47 PM 0
Share

seems easy enough, thanks man :)

avatar image EliteHedgehog56 · Apr 19, 2018 at 01:38 AM 0
Share

@shadowpuppet I didn't have to convert it to javascript, the damage message goes to my javascript health script just fine. If I wanted to make a railgun using the same script how do I make the raycast visible ?

avatar image shadowpuppet · Apr 19, 2018 at 09:01 PM 0
Share

not sure how to make the raycast visible. $$anonymous$$aybe a Line Render from the ray origin to the object it is hitting https://docs.unity3d.com/$$anonymous$$anual/class-LineRenderer.html I have ,on one of my guns, where I shoot tracers bullets. so in addition to the raycast I instantiate a tiny glowing sphere that has a Trail Rnderer component on it https://docs.unity3d.com/$$anonymous$$anual/class-TrailRenderer.html

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

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

shooting raycast 1 Answer

Best way to shoot physical bullets? 2 Answers

Multiplayer FPS Bullets: should they be rigid bodies or raycast? 1 Answer

fps shooting enemy 1 Answer

RayCast Shooting And Errors 1 Answer


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