- Home /
Bullets Shot Stay Still
Hi, I am passing an old shooting script that I made in JS to C#, and is working, except that the bullets just appear and aren't shot, where is my mistake? I know it worked almost a year ago.
Gun Script:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public float holdHeight= -0.5f;
public float holdSide = 0.5f;
public float racioHipHold = 1f;
public float hipToAimSpeed = 0.1f;
private float racioHipHoldV ;
public float aimRacio = 0.4f;
public float zoomAngle = 30f;
public float fireSpeed = 15f;
private float waitTilNextFire = 0f;
public GameObject bullet;
public GameObject bulletSpawn;
public float shootAngleRandomizationAiming = 5f;
public float shootAngleRandomizationNotAiming = 15f;
public float recoilAmount = 0.5f;
public float recoilRecoverTime= 0.2f;
private float currentRecoilZPos;
private float currentRecoilZPosV;
public GameObject bulletSound;
public GameObject muzzleFlash;
public GameObject muzzleFlashSpawn;
//Unlocks
public bool weaponUnlocked = false;
void Update ()
{
if (Input.GetButton("Fire1"))
{
if (waitTilNextFire <= 0)
if (weaponUnlocked == true)
{
GameObject holdSound = null;
GameObject holdMuzzleFlash = null;
if (bullet)
{
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if (bulletSound)
{
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation) as GameObject;
}
if (muzzleFlash)
{
holdMuzzleFlash = Instantiate(muzzleFlash, muzzleFlashSpawn.transform.position, muzzleFlashSpawn.transform.rotation) as GameObject;
}
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
if (holdSound)
{
holdSound.transform.parent = transform;
}
if (holdMuzzleFlash)
{
holdMuzzleFlash.transform.parent = transform;
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
currentRecoilZPos = Mathf.SmoothDamp(currentRecoilZPos, 0f, ref currentRecoilZPosV, recoilRecoverTime);
}
}
if you do not have a rigidbody attached to your bullet or created on the bullet, there is no way a force can be applied.
link to read unity manual, and have a sample code: click here
not sure that link worked so here it is written out:http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html or you can go to the scripting and type :Rigidbody.AddForce
agree with the above. add a script to your bullet that moves it. the easiest is with rigidbody.moveposition if you're using unity 4.5, but you can move an object without a rigidbody if you use vector3.lerp
Answer by robertbu · Jul 09, 2014 at 06:24 PM
There's no code here that moves the bullet. Should the bullet prefab have a script that moves it? If so, that would be the place to look for the problem. If not, you need to take a closer look at your translation.
Thanks man, I had a nother script which moved the bullet which I completely forgot about.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153631im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2d game facing bullets to mouse 2 Answers
what's the deal with the new (currently experimental) scripting system 0 Answers
C# Destroy tag on collision 1 Answer