- Home /
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.
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--;
}
}
}
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--;
}
}
}
}
Thanks for your help! I had to tweak sarthakshah's code, and your's gave me what I needed to do it!
Your answer
![](https://koobas.hobune.stream/wayback/20220613163109im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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