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.