- Home /
transform.forward.speed not working?
im wanting an object(bullet) to spawn and go at a certain speed straight away. i have a variable and im using rigidbidy.addforce(transform.forward speed); speed is my variable. it works fine when i use rigidbidy.addforce(transform.forward a number); but it doesnt work with my variable the object just stays still? heres my script:
#pragma strict
var speed : int;
var hole : GameObject;
var bulletpos : Transform;
var bulletspeedupgrade : int;
function Start () {
bulletpos = this.transform;
rigidbody.AddForce (transform.forward * speed);
}
function OnCollisionEnter () {
Instantiate(hole, bulletpos.position, bulletpos.rotation);
Destroy(this.gameObject);
yield WaitForSeconds (0.5);
}
function Update () {
bulletspeedupgrade = PlayerPrefs.GetInt("bulletspeedupgrade");
if (bulletspeedupgrade == 0) {
speed = 800;
}
if (bulletspeedupgrade == 1) {
speed = 1600;
}
if (bulletspeedupgrade == 2) {
speed = 2400;
}
if (bulletspeedupgrade == 3) {
speed = 3200;
}
if (bulletspeedupgrade == 4) {
speed = 4000;
}
}
hey thx alot it works ! I didnt realise it was that simple i was looking for something more difficult haha. thanks again!
Remove the code from Update and put it in Start. It's not doing anything right now. Also you can get rid of all the if statements by using an array.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163314im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody2D linearDrag - Suddenly Change 0 Answers
`Inventory' does not contain a definition for `Add' 2 Answers
Clones AddForce individual 1 Answer
addforce causing object to teleport. 0 Answers
fire to camera 1 Answer