2016-07-04 4 views
-1

현재 코드 작성 중입니다. 그러나, if 문을 닫는 중괄호 대신에 화면 오른쪽 뒤쪽에있는 걸림쇠로 실행되었습니다. 전체 코드를 닫습니다. 아무도 그 이유를 말할 수 있습니까? 이 부분 앞의 중괄호는 잘 작동합니다.중괄호 문제

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

이 적절한 깨끗하게 포맷 코드를 가진 것은이 C#을하지 자바 스크립트 스타일의 코딩입니다 .. 그것은 당신이 쓴 무엇 해결하는 방법을 이해하려고 노력하십시오 .. 당신을 도움이 될 곳이다 .. – MethodMan

답변

4

내가 완전히 코드를 이해하지 못했지만 패턴을 관찰함으로써, 난 당신이 내가 댓글을 달았 위치에서 몇 브래킷을 놓칠 수 있습니다 의심 : 다음은 내 코드입니다.

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 }?