Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class BallPhysics : MonoBehaviour {
- private float ballSpeed; // скорость шарика
- Rigidbody rb; // rigidbody объекта
- private bool up; // летишь вверх или вниз
- private bool right; // то, в какую сторону ты летишь в 0.3 секунды бездействия
- private float timer1; // таймер, отвечающий за кулдаун после удара
- public Vector3 HorizontalDirection, VerticalDirection;
- public float timer3; // таймер, отвечающий за кулдаун разрушения блоков
- public bool iced; // пока не используется
- public static bool Dead; // если игрок мёртв, игнорируем всю физику
- void Start () {
- Dead = false;
- rb = GetComponent<Rigidbody>();
- }
- void Update () {
- if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
- ballSpeed = 13.5319148936f;
- else
- ballSpeed = 6.76595744682f;
- if (!Dead) {
- if (timer1 == 0) { // все дальнейшие проверки происходят только вне 0.3 секунд кулдауна
- if (!iced) { // работает всегда, так как iced не используется
- if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
- HorizontalDirection = new Vector3 (-0.3f, 0, 0);
- if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
- HorizontalDirection = new Vector3 (0.3f, 0, 0);
- VerticalDirection = new Vector3(0.0f, up ? ballSpeed : -ballSpeed, 0.0f);
- rb.velocity = VerticalDirection + HorizontalDirection;
- }
- } else {
- // после 0.3 секунд игроку возвращается контроль над шаром
- if (timer1 > 0)
- timer1 -= Time.deltaTime;
- if (timer1 < 0) {
- timer1 = 0;
- }
- if (timer1 > 0.3f)
- timer1 = 0.3f;
- }
- } else {
- rb.velocity = Vector3.zero; // если игрок мёртв, то он остаётся на месте
- }
- if(timer3>0){
- timer3-=Time.deltaTime;
- } else {
- timer3=0;
- }
- if(timer3>0.01f){
- timer3=0.01f;
- }
- }
- void OnCollisionEnter(Collision col){
- if (col.collider.CompareTag("Cube")) {
- if((Mathf.Abs(col.transform.GetComponent<Transform>().position.x-GetComponent<Transform>().position.x)< 2.151f)) {
- if (timer1 > 0)
- rb.velocity = (new Vector3 (right ? 6.0f : -6.0f, (ballSpeed > 7) ? (up ? -12 : 12) : (up ? -6 : 6), 0));
- up = col.transform.position.y <= transform.position.y;
- } else {
- timer1 = 0.3f;
- right = col.transform.GetComponent<Transform>().position.x <= GetComponent<Transform>().position.x;
- rb.velocity = (new Vector3 (right ? 6.0f : -6.0f, (ballSpeed > 7) ? (up ? 12 : -12) : (up ? 6 : -6), 0));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement