- Home /
Shooting Pistol and Machine Gun
I have a pistol that shoots but it won't shoot when i Turn it just goes down one axis no matter which way I'm facing.
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate(newObject, transform.position, transform.rotation);
audio.Play();
}
}
How can I get it to change to shoot where ever i'm facing?
Also how would I change this to where it keeps firing as I hold down the mouse button like a machine gun?
This script is on the character correct? also what script is on newObject?
The Script is on the character. the new object is.....
//Bullet //how fast does the shot move var speed:float; var damage:int=-1; function Update() { //each frame move forward by speed units per second transform.position.z += speed *Time.deltaTime; }
//gets called when we hit something function OnTriggerEnter(other:Collider) {
//what are we hitting? we are only interested in hitting "enemy" if(other.gameObject.CompareTag("Enemy")) { //send a messege telling the object to take damage
other.gameObject.Send$$anonymous$$essage("OnDamage", damage); //destroy object Destroy(gameObject); } }
ok i took out the velocity on the bullet script and now the bullets shoot where the gun is pointed but they just sit there
Answer by oliver-jones · Dec 02, 2010 at 02:03 AM
Try this:
var projectile : Rigidbody; private var speed = 5;
function Update(){ if(Input.GetButtonDown("Fire1")) {
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity =
transform.TransformDirection(Vector3(speed, 0, 0));
}
Give your object that you are firing a rigidbody (then turn off gravity)
-----OR-----
if could be that you haven't placed your 'firing object' into your camera (thus changing its position with your players)
that didn't do it. The pistol does rotate and follow around with the camera. the bullets just dont
at some point I had made it kinematic and that game me an error. now it works.
Answer by drfudgemuffin · Feb 15, 2013 at 10:48 PM
rotate the bullet to face forwards that fixed for me
Your answer
Follow this Question
Related Questions
My gun won't fire. ( Javascript ) 1 Answer
dynamic_bitset.test bit out of bounds 0 Answers
Gun Firing help? 2 Answers
Gun Script glich? 1 Answer
Multiple Animations, Please Help 1 Answer