1) Construction of the form dir = Vector(0, 1); does not work because you need to write Vector2/Vector3 , and not just Vector . And since you do not access the property like here
Vector2.down;
it means that you need to refer to the class instance need to write
dir = new Vector2(0, 1);
2) Even if you write new Vector2(0, 1) in theory, it should fly strictly upwards, but not at an angle. To do this, you need to either rotate the object or specify new Vector2(1, 1) , i.e. fly up and to the right, i.e. at an angle
3) When bouncing off the sides at the same angle, it’s enough to remember that when one reaches one side, the speed changes to the opposite (without taking into account friction forces, rebound forces and so on). A la the angle of incidence equals the angle of reflection. So, when reaching the right or left side (i.e., the X axis), it is enough to change the direction of the movement to the opposite for the given axis: X = -X; . And since he (object) vertically, so will move in a given direction, you get a rebound angle. With a vertical rebound, everything is exactly the same.
From the above follows:
Vector2 dir; Transform myTransform; public float vx = 1f; public float vy = 1f; public float speed = 1f; void Start() { //InvokeRepeating("Move", 0.07f, 0.07f); myTransform =transform; dir = new Vector2(vx, vy); } void Update() { myTransform.Translate(dir * speed * Time.deltaTime); } // void Move() { transform.Translate(dir * speed * Time.deltaTime); } void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.name.StartsWith("bright") || coll.gameObject.name.StartsWith("bleft")) { vx = -vx; } if (coll.gameObject.name.StartsWith("btop") || coll.gameObject.name.StartsWith("bbottom")) { vy = -vy; } dir = new Vector2(vx, vy); }
4) About the randomness. You can use two things.
For example instead
vx = -vx;
can write
var sign = Mathf.Sign(vx); var random = Random.Range(1f, 2f); vx = random * sign * -1;
vx will take a value in the range from 1 to 2. sign - you need it here to set the correct value of vx , because we don’t know at this time that it has a value: positive or negative. And so we take a sign, we generate a number, we assign it with the opposite sign.
5) Regarding the angles of rotation, Unity has many different options and it is easier to deduct everything yourself. Someone makes different X and Y parameters and as a result, an angle is reached, someone uses transform.localRotation , someone uses the transform.Rotate () method. There is even an example:
public class FollowPath: MonoBehaviour { public float speed; public float rotationSpeed; //transform Transform myTrans; //object position Vector3 myPos; //object rotation Vector3 myRot; //object rotation float angle; // Use this for initialization void Start() { myTrans = transform; myPos = myTrans.position; myRot = myTrans.rotation.eulerAngles; } // Update is called once per frame void FixedUpdate() { //converting the object euler angle's magnitude from to Radians angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad; //rotate object Right & Left if (Input.GetKey(KeyCode.RightArrow)) { myRot.z -= rotationSpeed; } if (Input.GetKey(KeyCode.LeftArrow)) { myRot.z += rotationSpeed; } //move object Forward & Backward if (Input.GetKey(KeyCode.UpArrow)) { myPos.x += (Mathf.Cos(angle) * speed) * Time.deltaTime; myPos.y += (Mathf.Sin(angle) * speed) * Time.deltaTime; } if (Input.GetKey(KeyCode.DownArrow)) { myPos.x += Mathf.Cos(angle) * Time.deltaTime; myPos.y += Mathf.Sin(angle) * Time.deltaTime; } //Apply myTrans.position = myPos; myTrans.rotation = Quaternion.Euler(myRot); } }
Where the movement itself is built on the calculation of the angle and substitution in the transform.position
myPos.x += (Mathf.Cos (angle) * speed) * Time.deltaTime; myPos.y += (Mathf.Sin (angle) * speed) * Time.deltaTime;
For your example, you can try something like this:
private Vector2 dir; private Transform myTransform; public float speed; public float angle; public float vx = 1; public float vy = 1; void Start() { myTransform = transform; InvokeRepeating("Move", 0.05f, 0.05f); dir = Vector2.right; angle = Vector2.Angle(Vector2.right, new Vector2(vx, vy)); myTransform.localRotation = Quaternion.Euler(0, 0, angle); } void Move() { myTransform.Translate(dir * speed * Time.deltaTime); } void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.name.StartsWith ("bright") || coll.gameObject.name.StartsWith("bleft")) { vx = -vx; } if (coll.gameObject.name.StartsWith ("btop") || coll.gameObject.name.StartsWith("bbottom")) { vy = -vy; } angle = Vector2.Angle(Vector2.right, new Vector2(vx, vy)); if (coll.gameObject.name.StartsWith ("bleft") && vy < 0 || coll.gameObject.name.StartsWith ("bright") && vy < 0 || coll.gameObject.name.StartsWith("btop")) { angle = -angle; } myTransform.localRotation = Quaternion.Euler(0, 0, angle); }

But it is better to read more about the rotation and how to optimize them and apply to yourself.