Hi i am Creating simple bouncing ball 2D game to learn some basics. Applying Random directional force on Ball. but when bouncing speed gets high over the time the ball flips Pad collider/RigidBody2D attached and sometimes it goes out of the walls due to high speed i assume.
What i understood in first place was that it might have move a bit in z Axis then Froze in z Axis in Constraints tab. But yet again to no avail.
Here is Video
Please Guide me what could be Wrong.
Here are my scripts.
Ball Scripts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
Rigidbody2D rb;
Vector2 direction;
float xDir;
float yDir;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
direction = new Vector2(xDir, yDir);
yDir = 1f;
xDir = 0f;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
print("Death");
}
if (collision.gameObject.tag == "Pad")
{
rb.AddForce(direction * Time.deltaTime * 500f, ForceMode2D.Impulse);
}
}
}
Pad Script
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
Rigidbody2D rb;
Vector2 touchPos;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
PaddleMove();
}
void PaddleMove()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
touchPos = Camera.main.ScreenToWorldPoint(touch.position);
rb.MovePosition(touchPos);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ball")
{
}
}
}
__```*__