I've been trying to add a weapon script that make's my weapon shoot when I left click on my mouse but something is wrong because my weapon doesn't shoot when I click...
anyone got a simple working script that I can use ?
Thanks!
I've been trying to add a weapon script that make's my weapon shoot when I left click on my mouse but something is wrong because my weapon doesn't shoot when I click...
anyone got a simple working script that I can use ?
Thanks!
Try This Guys Its SImple But Its Work For Me… :3
var Bullet : Transform;
var Spawn : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
function Shot()
{
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 8000);
}
I used it for my dart gun:
var gasPrefab : Transform;
privatevar dart;
var gas;
var buttonFire_pressed : boolean;
var ammo : int;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
if (ammo>0)
{
if (audio)
{
audio.Play();
audio.loop = false;
}
var player_camera = GameObject.Find("MainCamera");
var ray : Ray =player_camera.camera.ViewportPointToRay(Vector3(0.5,0.5,0));
var hit : RaycastHit;
//dart impact
if (Physics.Raycast (ray, hit))
{
gas = Instantiate(gasPrefab, hit.point,
GameObject.Find("wpn_poison_2_spawn_point").transform.rotation);
gas.name = "dart_poisongas"+Time.time;
}
}
ammo--;
}
}
var Bullet : Transform;
var Spawn : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
function Shot()
{
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 8000);
}
this script also works but you have to name it “ShootDemo”
using UnityEngine;
using UnityEngine;
using System.Collections;
public class ShootDemo : MonoBehaviour {
public Rigidbody projectile;
public float speed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}