我正在处理我的代码,但是我在//关闭屏幕之后遇到了障碍,而不是关闭我的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 :(得分:4)
我没有完全理解你的代码,但通过观察模式,我怀疑你可能会错过我所评论的位置的一些支架。
height: 0;
&#13;