- Home /
Fire script not working
using UnityEngine;
using System.Collections;
public class Fire : MonoBehaviour {
public GameObject whiteCell;
public string fireButton = "Fire1";
public float shotPower = 1f;
public int ammo = 20;
// Use this for initialization
void Start () {
Instantiate (whiteCell, new Vector3(transform.position.x,-2.5f, 0), Quaternion.identity);
}
// Update is called once per frame
void Update () {
//while (ammo > 0){
//if (Input.GetButtonDown(fireButton)){
// Debug.Log("Hello");
// //ammo--;
//}
//whiteCell.transform.position += new Vector3(0, 0.1f, 0);
whiteCell.rigidbody.AddForce(transform.up * shotPower);
//}
}
}
I'm trying to shoot a "bullet" that will go directly upwards on the y-axis. Unfortunately this is not working and I cannot get my head round why this is the case I tried using the transform.position method and the rigidbody.AddForce method but both are not giving me results.
I'm sure there is something wrong with my code but I can't find what exactly is wrong. Any insight will be greatly appreciated. Thanks :)
Answer by robertbu · Dec 08, 2013 at 06:59 PM
'transform.up' is the 'up' of the object this script is attached to. Vector3.up is what you are looking for:
whiteCell.rigidbody.AddForce(Vector3.up * shotPower);
Note if you are talking about going directly up the axis, then you need to Instantiate() or position the object on the axis ('x' and 'z' == 0), but you cannot do it at every update call. I'm not sure where you are going with this code, but try this:
if (Input.GetMouseButtonDown(0)) {
whiteCell.transform.position = Victor3.zero;
whiteCell.rigidbody.AddForce(Vector3.up * shotPower);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613123722im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
my prefab is null woe 2 Answers
How to do health thru network 1 Answer
Shoot towards mouse in Unity2D 2 Answers
Ultimate FPS not working correctly. 0 Answers
Shooting 2D - how to fix 1 Answer