2016-07-04 5 views
-1

Ich arbeite gerade an meinem Code, aber ich habe in einen Haken geraten nach // Off Bildschirm auf der rechten Seite anstelle der geschweiften Klammer Schließen meiner if-Anweisung es schließt den gesamten Code. Kann mir jemand sagen warum? Die geschweiften Klammern vor diesem Teil funktionieren einwandfrei. Unten ist mein Code:Curly Bracket Probleme

public class ShipPlayerController : MonoBehaviour { 

//The reference to the bullet prefab. To be populated in the editor via the Inspector window. 
public Projectile Bullet; 

//Determines the screen bounds in relation to the player object. This camera object needs to e populated this attribute in the Inspector 
public Camera CameraObject; 

//Control how quickly we can fire (delay between shots) 
public float FireRate = 0.5f; 

//The amount of time since we last fired our weapon 
private float FireTimer = 0.0f; 

//Used to control how fast the ship moves 
[Range(1f, 100f)] 
[SerializeField] 
public float MoveSpeed = 25.0f; 

//Used to control how fast the ship moves when the t button is pressed 
[Range(1f, 100f)] 
[SerializeField] 
public float ThrusterSpeed = 50.0f; 

//Helper vector for updating the ship's position declared here since it is used every update/tick and we do not want to waste CPU power recreating it constantly. 
private Vector3 movement = Vector3.zero; 

public Rigidbody rb; 

// Use this for initialization 
void Start() { 
    rb = GetComponent<Rigidbody>(); 
} 

// Update is called once per frame 
void Update() { 

    UpdatePosition(); 
    UpdateFiring(); 
    FixedUpdate(); 

} 

//Update the position of the ship based on the "Horizontal" and "Vertical" input 
void UpdatePosition() { 
    //Move the player laterally in the 'X' coordinate 
    movement.x = Input.GetAxis("Horizontal") * Time.deltaTime * MoveSpeed; 

    //Move the player laterally in the 'Y' coordinate 
    movement.y = Input.GetAxis("Vertical") * Time.deltaTime * MoveSpeed; 

    //Apply the movement vector to the game object's postion 
    gameObject.transform.Translate(movement); 

    //Transform the 3D world position to a screen pixel location 
    Vector3 screenPosition = CameraObject.WorldToScreenPoint(
           gameObject.transform.position); 
} 

    //Off screen to the RIGHT 
    if (screenPosition.x > Screen.width) { 

     //Clamp (reset) to the screen's right side 
     screenPosition.x = 0; 

     //Transform clamped screen position to the world space and assign to player ship 
     gameObject.transform.position = CameraObject.ScreenToWorldPoint(screenPosition); } 

    //Off screen to the LEFT 
    else if (screenPosition.x< 0) 

     //Clamp (reset) to the screen's left side 
     screenPosition.x = Screen.width; 

     //Transform clamped screen position to world space and assign player to ship 
     gameObject.transform.position = CameraObject.ScreenToWorldPoint(screenPosition); 

    //Off screen to the TOP 
    if (screenPosition.y > Screen.width) 
     //Clamp (reset) to the screen's top side 
     screenPosition.y = 0; 

     //Transform clamped screen position ti the world space and assign to the player ship 
     gameObject.transform.position = 
      CameraObject.ScreenToWorldPoint(screenPosition); } 

    //Off screen to the BOTTOM 
    else if (screenPosition.y< 0) { 

     //Clamp (reset) to the screen's bottom side 
     screenPosition.y = 0; 

     //Transform clamped screen position to the world space and assign player to the ship 
     gameObject.transform.position = 
      CameraObject.ScreenToWorldPoint(screenPosition); 

    void FixedUpdate() 
    if (Input.GetKey.(KeyCode.T)) { 

    rb.AddForce(0, 0, ThrusterSpeed, ForceMode.Impulse); } 


//Update the firing of the ship based on "Fire1" inout 
void UpdateFiring() { 

//Accumulate time each frame, when the fire key is pressed, we check if enough time has passed. 
FireTimer += Time.deltaTime; 

//Detect if the fire button has been pressed. 
    if (Input.GetButton("Fire1")) { } 

    //Has the fire timer exceeded the amount o time between the spawning of projectiles? 
    if (FireTimer > FireRate) { } 

     //Reset the timer so it will start counting from scratch for the next shot. 
     FireTimer = 0; 

//Call the function which handles the spawning of projectiles. 
DoWeaponFire(); } 

//Handles the spawning of the projectile 
void DoWeaponFire() { 

//Create a new instance of the bullet and place it at the location of the player, facing in the same direction. 
Instantiate(Bullet, transform.position, transform.rotation); } 

}

+0

Dies ist, wo die richtige sauber formatiert Code mit Ihnen helfen würde .. bitte versuchen Sie es, wie zu verstehen, zu beheben, was es ist, geschrieben haben .. das C# kein Javascript Stil Codierung ist .. – MethodMan

Antwort

4

ich nicht ganz Code verstanden, sondern durch das Muster zu beobachten, vermute ich Ihnen ein paar Bügel an der Position verpassen kann ich kommentiert habe.

if (screenPosition.x > Screen.width) { 
 

 
     screenPosition.x = 0; 
 

 
     gameObject.transform.position = CameraObject.ScreenToWorldPoint(screenPosition); } 
 

 
    else if (screenPosition.x< 0) // <--- need { ? 
 

 
     
 
     screenPosition.x = Screen.width; 
 

 
     
 
     gameObject.transform.position = CameraObject.ScreenToWorldPoint(screenPosition); 
 
    // <--- need } ? 
 
    
 
    if (screenPosition.y > Screen.width) // <--- need { ? 
 
     screenPosition.y = 0; 
 
     gameObject.transform.position = 
 
      CameraObject.ScreenToWorldPoint(screenPosition); } 
 

 
    else if (screenPosition.y< 0) { 
 

 
     
 
     screenPosition.y = 0; 
 
     gameObject.transform.position = 
 
      CameraObject.ScreenToWorldPoint(screenPosition); 
 
     // <-- need }?