Setting random velocity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

public int speed = 15;
Rigidbody2D ball;
public float change = 100.0f;

    // Use this for initialization
    void Start () {
       
        ball = GetComponent<Rigidbody2D>();
    }
   
    // Update is called once per frame
    void FixedUpdate () {
       
        ball.velocity = new Vector3(speed,0,0);
    }

    void OnCollisionEnter2D (Collision2D other) {

        speed = -speed;
        ball.velocity = new Vector2(ball.velocity.x,Random.Range(-change, change));
    }
}

As you can see, in OnCollisionEnter2D I’m trying to set a random Y velocity, but it does nothing. What’s wrong?

for the y value, eh? It’s not that it’s not doing nothing (Unless your collision is not actually working, which is a different matter).
The thing is that you’re setting the velocity to (speed,0,0) in FixedUpdate, thus overriding your change. :wink: