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 caleb_b · Sep 16, 2014 at 04:09 AM · bullet

Change this JS gun shooting script into working C#

I got this JS script off of a Youtube tutorial I took a while back for a FPS game. This was back when I had just started in game development. Since then, I've started writing in C#. I just started a new Shooter project, and I want the scripts to be all the same. I liked how it worked in JS, so I tried to translate it to C#, but to no avail. Here is the original JS version:

 var sparks : GameObject;
 var ammo : int = 100;
 var damage : int = 1;
 var fireRate : float = 0.0;
 
 private var nextFire : float = 0.0;
 
 function Update()
 {
       if(Input.GetButton("Fire1")&& ammo>0)
       {
            if(Time.time > nextFire)
            {
                nextFire = Time.time + fireRate;
                fire();
            }
       }
 }
 
 function fire()
 { 
      var range = Mathf.Infinity;
      var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      var hit : RaycastHit;
      if(Physics.Raycast(ray, hit, range))
      {
          sparks.transform.position = hit.point;
          ammo--;
      }
 }

And here is my miserable Frankenstein of a C# translation:

 using UnityEngine;
 using System.Collections;
  
 public class PlayerGunController : MonoBehaviour 
 {
     public GameObject projectile;
     public int speed = 10;
     public int ammo;
     public RaycastHit hit;
  
  
  
     void Update() 
     {
         Vector3 fwd =  transform.TransformDirection(Vector3.forward);
         if(Input.GetMouseButtonDown(0)) 
         {
             if(Physics.Raycast(transform.position, fwd, Mathf.Infinity))
             {
                 projectile.transform.position = hit.point;
             }
         }
     }
 }

Honestly, gents, I'm at a loss here. I have a gun model parented to the main camera from the stock FPS controller, with a capsule parented to the gun to act as the "projectile" in the above script. As this script stands, I click the button, and the capsule teleports to a point below my terrain, and moves about, still parented to the gun, and not at all functioning the way it did in JS. Let me know if there are any other details I need to supply.

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

2 Replies

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

Answer by sarthakshah · Sep 16, 2014 at 05:12 AM

I have converted your Js into C#....

 using UnityEngine;
 using System.Collections;
 
 public class MYCLASSNAME : MonoBehaviour {
 public GameObject sparks;
 public int ammo = 100;
 public int damage = 1;
 public  float fireRate = 0.0f;
  
 private float nextFire = 0.0f;
  
 void  Update (){
       if(Input.GetButton("Fire1")&& ammo>0)
       {
            if(Time.time > nextFire)
            {
                nextFire = Time.time + fireRate;
                fire();
            }
       }
 }
  
 void  fire (){ 
      float range= Mathf.Infinity;
      Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;
      if(Physics.Raycast(ray, hit, range))
      {
          sparks.transform.position = hit.point;
          ammo--;
      }
  }
 }
Comment
Add comment · Show 1 · 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 caleb_b · Sep 16, 2014 at 12:30 PM 0
Share

Couple of tweaks, and your translation works! Thank you

avatar image
1

Answer by robertbu · Sep 16, 2014 at 04:21 AM

Your original (JS) code causes 'projectile' to be places at the position of a hit based on a mouse click. The (untested) C# code below duplicates that functionality. Note in your C# code, you 1) are not using the mouse position in your raycast, and 2) are not using a version of Raycast that takes a RaycastHit structure. Without passing 'hit', to Raycast(), 'hit' is not initialized.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerGunController : MonoBehaviour 
 {
     public GameObject projectile;
     public int ammo = 100; 
     
     void Update() 
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Input.GetMouseButtonDown(0)) 
         {
             if(Physics.Raycast(ray, out hit))
             {
                 projectile.transform.position = hit.point;
                 ammo--;
             }
         }
     }
 }
Comment
Add comment · Show 1 · 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 caleb_b · Sep 16, 2014 at 12:33 PM 1
Share

Thanks for your help! I had to tweak sarthakshah's code, and your's gave me what I needed to do it!

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

24 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

Related Questions

Bullet timer using + time.deltaTime. 2 Answers

how to make a bullet reflect off a wall? 4 Answers

add force on topdown bullet 1 Answer

Bullet impact effect not working. Made from a Brackeys tutorial 2 Answers

Time effected Bullets 0 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