- Home /
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
Please edit your question, select the code, and hit the 101/010 button to format it as code. Thanks
Answer by DaveA · Jun 08, 2011 at 12:20 AM
There is nothing in this code that indicates it will shoot anything. Move, rotate, ignore Jump, that's all.
Answer by Dreamer · Jun 08, 2011 at 02:00 AM
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.
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'.