x


Help with script

Heres my script its soposed to shoot but im missing something can some one help me its Javascript

var bulletPrefab:Transform;

var speed = 3.0;

var rotateSpeed = 3.0;

function Update ()
{ 
    var controller : CharacterController=GetComponent(CharacterController);

    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = speed * Input.GetAxis ("Vertical");

    controller.SimpleMove(forward * curSpeed);
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

     if(Input.GetButtonDown("Jump"));
}       

what am i missing to make it want to shoot then or can you point me in the right way tornadotwins didnt help they dont use javascript

more ▼

asked Jun 08 '11 at 12:18 AM

colopto gravatar image

colopto
11 6 11 13

Please edit your question, select the code, and hit the 101/010 button to format it as code. Thanks

Jun 08 '11 at 12:19 AM DaveA

Tornado twins do use java script in there tutorials..

Jun 08 '11 at 09:26 PM Eli Davis
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

There is nothing in this code that indicates it will shoot anything. Move, rotate, ignore Jump, that's all.

more ▼

answered Jun 08 '11 at 12:20 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left

First of all, bulletPrefab is your bullet prefab, it needs to be declared as GameObject. Then in your character control script, you need to do so when your mouse is clicked. It will instantiate a bullet prefab in correct direction. See below:

var bulletPrefab:GameObject;
var speed = 3.0;
var rotateSpeed = 3.0;

function Update ()
{ 
    var controller : CharacterController=GetComponent(CharacterController);

    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = speed * Input.GetAxis ("Vertical");

    controller.SimpleMove(forward * curSpeed);
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

    if(Input.GetButtonDown("Jump"))
    //put your jump function below
    print("I jumped!");

    if(Input.GetButtonDown("Shoot"))
    Instantiate(bulletPrefab,transform.posistion,Quaternion.FromToRotation(transform.posistion, forward );
}   

You also need to create a script to control movement of your bullet. Put below on your bullet prefab:

var speed = 3.0;

function Update(){
 transform.Translate(Vector3.up *speed* Time.deltaTime, Space.World);
}

That's basically the idea how to shoot a bullet out.

more ▼

answered Jun 08 '11 at 02:00 AM

Dreamer gravatar image

Dreamer
1.5k 41 50 66

Unless you need to see the bullet move (slow motion), why use them like that at all? Probably easier to cast a ray for 'instant hit'.

Jun 08 '11 at 08:03 PM DaveA

because he has a bulletPrefab in his script.

Jun 09 '11 at 01:35 AM Dreamer
(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:

x461
x320

asked: Jun 08 '11 at 12:18 AM

Seen: 750 times

Last Updated: Jun 09 '11 at 01:35 AM