Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to Sync bullet spawn with shoot animation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges