- Home /
Shootin Worminator Like Game
Hi guys i am making a game similar to worminator. However my shooting script doesnt work in that my character doesnt even fire the bullet although movement works, can you have a look at it and see whats wrong.
var speed = 3.0;
var rotateSpeed = 3.0;
var bullitPrefab:Transform;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
var Bullet : Transform;
function (
(Input.GetButtonDown("Jump"))
{
Instantiate(Bullet,transform.position,Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 1000);
}
}
Thanks
I added the code into a code block to make it readable - For next time, please select the code after you've pasted it, and click the button with 0's and 1's on it (like 110101, i forget exactly)
Answer by FLASHDENMARK · Feb 01, 2011 at 06:01 PM
Mabye like so:
var speed = 3.0; var rotateSpeed = 3.0; var Bullet : Transform;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Fire1"))
{
Bullet = Instantiate(Bullet,transform.position,Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 1000);
}
}
Good old TornadoTwins, he is awesome ;)
ok it works apart from it says the thing you want ot instatiate is nul?
I have found the error. At the end of the script above the "if statement" There you declared a variable(var) but you had also declared it at the top. I assume that Unity did not like that. I will update the script :)
That'll cause null ref exceptions every time after the first instantiation
Answer by Mike 3 · Feb 01, 2011 at 02:52 AM
This should fix it:
var Bullet : Transform;
if (Input.GetButtonDown("Jump"))
{
Bullet = Instantiate(bullitPrefab,transform.position,Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 1000);
}
First change is that it needs to use if, not function, to check if the key is down
Second is that you need to assign the Instantiate to the Bullet prefab so you can use it after
Third is that you need to Instantiate bullitPrefab, not Bullet (which is completely empty still)
It doesnt seem to work :( firstly i see that it says expectin } found " on 17,1. Secondly do i need to change the word bullit prefab to my bullet and thirdly do i attach it to the actual character?
ok so i filled the bullit prefab and thats ok, all i need to know now is why it wont actually run because of the "expecting } found " on 17,1."
You may want to edit your question with the entire code. Replacing from var Bullet : Transform up until the first } with my code should have fixed it
Answer by lolage · Feb 01, 2011 at 05:27 PM
It doesnt seem to work :( firstly i see that it says expectin } found " on 17,1. Secondly do i need to change the word bullit prefab to my bullet and thirdly do i attach it to the actual character?
Your answer
Follow this Question
Related Questions
Shoot Script Help 1 Answer
Hey can anyone help with a shooting script??? 0 Answers
simple shooting script help 3 Answers
Trouble with selective projectile script 0 Answers
Shooter in iOS and a visible Aim line before shooting 0 Answers