Во-первых, я извиняюсь за мой английский. Надеюсь, я смогу объяснить это правильно. В моей игре мяч должен отскакивать от края до края. Из видео у меня есть код с мячом в поле, должен остаться, однако мяч теперь подпрыгивает и скользит по экрану
Я пробовал утверждение if, но я новичок и незнать, правильно ли я понял
public class Ball_Controller : MonoBehaviour
{[SerializeField] private float moveSpeed = 10f;
[SerializeField] private Vector2 startDirection;
[SerializeField] private Vector2 startOtherDirection;
private Rigidbody2D rb;
public static bool GameIsover;
public GameObject gameOverUI;
public Transform topLeft;
public Transform bottomRight;
private void Start()
{
GameIsover = false;
}
void Update()
{
BallMove();
transform.position = new Vector3(Mathf.Clamp(transform.position.x, topLeft.position.x, bottomRight.position.x), Mathf.Clamp(transform.position.y, bottomRight.position.y, topLeft.position.y), transform.position.z);
if (transform.position.y == Screen.height || transform.position.x == Screen.width)
{
BallMoveOther();
}
if (GameIsover)
return;
if (Input.GetKeyDown("e"))
{
EndGame();
}
}
void EndGame()
{
Debug.Log("Game Over");
GameIsover = true;
gameOverUI.SetActive(true);
Time.timeScale = 0;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals ("Player"))
{
Destroy(collision.gameObject);
Destroy(gameObject);
EndGame();
}
}
private void BallMove()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = moveSpeed * startDirection;
}
private void BallMoveOther()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = moveSpeed * startOtherDirection;
}
}