Advertisement
Sibz

BallVelocity [GameDevExchange]

Nov 1st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BallVelocity2 : MonoBehaviour
  6. {
  7.     public Collider2D PaddleCollider;
  8.  
  9.     private Rigidbody2D m_BallRigidbody2D;
  10.     private Collider2D m_BallCollider;
  11.  
  12.     [Tooltip("Set the intial velocity")]
  13.     public Vector2 Velocity = new Vector2(1.1f,1.0f);
  14.  
  15.     [Tooltip("Percent increment per TimeIncrement. i.e. 2.5 = 2.5%")]
  16.     [Range(0f, 10f)]
  17.     public float Increment = 2.5f;
  18.  
  19.     [Tooltip("Time each increment is applid")]
  20.     [Range(0f, 20f)]
  21.     public float TimeIncrement = 3f;
  22.  
  23.     // Store these for use each fixed update
  24.     private Vector3 m_NewVelocityLeft, m_NewVelocityRight;
  25.  
  26.     // Store if we have rebounded this collision
  27.     private bool m_Rebounding = false;
  28.  
  29.     void Awake()
  30.     {
  31.         // Set references to require components
  32.         m_BallRigidbody2D = GetComponent<Rigidbody2D>();
  33.         m_BallCollider = GetComponent<Collider2D>();
  34.         m_NewVelocityRight = new Vector2(Velocity.x, Velocity.y);
  35.         m_NewVelocityLeft = new Vector2(-Velocity.x, Velocity.y);
  36.  
  37.  
  38.         m_BallRigidbody2D.velocity = new Vector2(Velocity.x, -Velocity.y);
  39.  
  40.         StartCoroutine(IncrementVelocity());
  41.     }
  42.  
  43.     private void FixedUpdate()
  44.     {
  45.         // Manually check we have collided with paddle or are about to
  46.         if (m_BallCollider.IsTouching(PaddleCollider))
  47.         {
  48.             // Check we are not currently rebounding (still touching collider after last)
  49.             if (!m_Rebounding)
  50.             {
  51.                 m_Rebounding = true;
  52.  
  53.                 float dist = transform.position.x - PaddleCollider.transform.position.x;
  54.  
  55.                 if (dist > 0)
  56.                 {
  57.                     print("right");
  58.                     // Set velocity once only in fixed update
  59.                     // This is so other standard collisions will work, i.e. walls
  60.                     m_BallRigidbody2D.velocity = m_NewVelocityRight;
  61.                 }
  62.                 else
  63.                 {
  64.                     print("left");
  65.                     m_BallRigidbody2D.velocity = m_NewVelocityLeft;
  66.                 }
  67.             }
  68.         }
  69.         else
  70.         {
  71.             // If not touching then we're clear of rebounding
  72.             m_Rebounding = false;
  73.         }
  74.     }
  75.  
  76.     public IEnumerator IncrementVelocity()
  77.     {
  78.         yield return new WaitForSeconds(1);
  79.         m_NewVelocityLeft += m_NewVelocityLeft * (Increment/100);
  80.         m_NewVelocityRight += m_NewVelocityRight * (Increment / 100);
  81.         StartCoroutine(IncrementVelocity());
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement