x


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

more ▼

asked Jan 31, 2011 at 09:26 PM

lolage gravatar image

lolage
3 3 2 6

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)

Feb 01, 2011 at 02:54 AM Mike 3
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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 ;)

more ▼

answered Feb 01, 2011 at 06:01 PM

OrangeLightning gravatar image

OrangeLightning
6.6k 127 119 174

ok it works apart from it says the thing you want ot instatiate is nul?

Feb 01, 2011 at 06:23 PM lolage

also do i need to put where it actually forms?

Feb 01, 2011 at 06:25 PM lolage

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 :)

Feb 01, 2011 at 07:27 PM OrangeLightning

That'll cause null ref exceptions every time after the first instantiation

Feb 02, 2011 at 01:29 AM Mike 3
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Feb 01, 2011 at 02:52 AM

Mike 3 gravatar image

Mike 3
33.2k 21 88 297

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?

Feb 01, 2011 at 05:47 PM lolage

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."

Feb 01, 2011 at 05:59 PM lolage

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

Feb 02, 2011 at 01:30 AM Mike 3
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Feb 01, 2011 at 05:27 PM

lolage gravatar image

lolage
3 3 2 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5000
x467
x436

asked: Jan 31, 2011 at 09:26 PM

Seen: 1293 times

Last Updated: Feb 01, 2011 at 02:49 AM