public class Ball : MonoBehaviour
{
public float speedBall = 200f;
Rigidbody2D rbBall;
private void Awake()
{
rbBall = GetComponent<Rigidbody2D>();
}
private void Start()
{
addStartingForce();
}
private void Update()
{
}
void addStartingForce()
{
float x = Random.value < 0.5f ? -1f : 1f; //less than half, go negative. Greater than half, go positive
float y = Random.value < 0.5f ? Random.Range(-1f, -0.5f) : Random.Range(0.5f, 1f);
Vector2 direction = new Vector2(x, y);
rbBall.AddForce(direction * speedBall);
}
public void addforce(Vector2 force)
{
rbBall.AddForce(force);
}
void HeadingTowards()
{
Vector2 Dir = new Vector2(-50f, 0f);
rbBall.AddForce(Dir * speedBall);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("wall"))
{
HeadingTowards();
}
}
}
Now once it bounces off a wall, it sometimes head towards my ("HeadingTowards") direction which was intended but after each bounce it didn't and the speed goes crazy fast unless I put time.deltaTime which I was told to not put it on addforce.