- Home /
Question by
magsjuco · Dec 11, 2013 at 03:57 AM ·
javascriptshootingbulletspaceship
help with bullet shooting code? - javascript
i purchased a tutorial from the assets store and everything is okay so far except for the bullet code. It's showing 5 errors on the console tab.
1. Assets/Scripts/PlayerController.js(50,10): BCE0044: expecting (, found 'shoot'.
2. Assets/Scripts/PlayerController.js(52,1): BCE0043: Unexpected token: var.
3. Assets/Scripts/PlayerController.js(52,4): UCE0001:';' expected. Insert a semicolon at the end.
4. Assets/Scripts/PlayerController.js(52,5): BCE0043: Unexpected token: bullet_position.
5. Assets/Scripts/PlayerController.js(56,1): BCE0044: expecting EOF, found '}'.
Here is what I have on my script so far (I don't know why it shows disorganized when I pasted it):
#pragma strict
public var player_speed : int = 10;
public var screen_top:float = 1;
public var screen_bottom:float = -8.5;
public var screen_left:float = -12.5;
public var screen_right:float = 12.5;
public var player_position_x:float;
public var player_position_y:float;
public var bullet:GameObject;
function Start ()
{
}
function Update ()
{
player_position_x = transform.position.x;
player_position_y = transform.position.y;
if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
if (player_position_y < screen_top)
{
transform.Translate (Vector3(0,0,-1) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.DownArrow)|| Input.GetKey(KeyCode.S))
{
if (player_position_y > screen_bottom)
{
transform.Translate (Vector3(0,0,1) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A))
{
if (player_position_x > screen_left)
{
transform.Translate (Vector3(1,0,0) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.RightArrow)|| Input.GetKey(KeyCode.D))
{
if (player_position_x < screen_right)
{
transform.Translate (Vector3(-1,0,0) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.Space))
{
shoot();
function shoot();
{
var bullet_position:Vector3 = transform.position;
var spawned_bullet:GameObject = Instantiate(bullet, bullet_position, Quaternion.identity);
}
}
}
Comment
Answer by robertbu · Dec 11, 2013 at 04:06 AM
Here is rework of your code so that it compiles. I think it is what you were going for. The issues were misplaced brackets. Plus on line 50 you have a ';' at the end of the line that needs to be deleted:
#pragma strict
public var player_speed : int = 10;
public var screen_top:float = 1;
public var screen_bottom:float = -8.5;
public var screen_left:float = -12.5;
public var screen_right:float = 12.5;
public var player_position_x:float;
public var player_position_y:float;
public var bullet:GameObject;
function Update ()
{
player_position_x = transform.position.x;
player_position_y = transform.position.y;
if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
if (player_position_y < screen_top)
{
transform.Translate (Vector3(0,0,-1) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.DownArrow)|| Input.GetKey(KeyCode.S))
{
if (player_position_y > screen_bottom)
{
transform.Translate (Vector3(0,0,1) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A))
{
if (player_position_x > screen_left)
{
transform.Translate (Vector3(1,0,0) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.RightArrow)|| Input.GetKey(KeyCode.D))
{
if (player_position_x < screen_right)
{
transform.Translate (Vector3(-1,0,0) * Time.deltaTime*player_speed);
}
}
if (Input.GetKey (KeyCode.Space))
{
shoot();
}
}
function shoot()
{
var bullet_position:Vector3 = transform.position;
var spawned_bullet:GameObject = Instantiate(bullet, bullet_position, Quaternion.identity);
}
Your answer
Follow this Question
Related Questions
Rapid fire Help 1 Answer
AddForce is not working 1 Answer
sending message to bullet (javascript) 1 Answer
Shooting style like enter the dungeon 0 Answers