Make my gun model fire bullets

I’ve done this before, when I first started with Unity, as a complete beginner to game development. The problem is that the tutorial I followed to do it was written in Javascript, and I write with C# now that I have more experience. I tried to translate the Javascript to C# but to no avail.

I snagged a model of a pistol off of the Asset Store, and threw this script on it.

using UnityEngine;
using System.Collections;

public class PlayerGunController : MonoBehaviour 
{
	public GameObject projectile;
	public int speed = 10;
	public int ammo;
	public RaycastHit hit;

	void Update() 
	{
		if(Input.GetKeyUp(KeyCode.S)) 
		{
			projectile.transform.position = hit.point;
		}
	}
}

As I’m sure you’ve guessed, it doesn’t work. I click the button and the capsule I assigned as “projectile” disappears(The way it is positioned, it sticks out notably from the gun model). I have no idea where it goes from there. I’ve tried Frame-by-frame but I just can’t see.

If I had to guess, I’d say the problem has a lot to do with the raycast, but I’m at a loss from there.

I appreciate any help. “Do this” type answers are good, but I’d prefer an explanation if that’s possible. Thanks peeps!

First of all, I don’t see where the raycast is actually cast :slight_smile: I assume you cast it when the gun is fired, and want the bullet to travel to the point that the ray hit. Ok so far, but you only set the bullet’s transform.position without any smooth movement. You can use Vector3.Lerp, for example. It’s explained further in the Unity Scripting API.