-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallControl.cs
More file actions
42 lines (36 loc) · 998 Bytes
/
Copy pathBallControl.cs
File metadata and controls
42 lines (36 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour
{
private Rigidbody2D rb2d;
void GoBall() {
float rand = Random.Range(0, 2);
if (rand < 1) {
rb2d.AddForce(new Vector2(20, -15));
}
else {
rb2d.AddForce(new Vector2(-20, -15));
}
}
// Start is called before the first frame update
void Start(){
rb2d = GetComponent<Rigidbody2D>();
}
void ResetBall() {
rb2d.velocity = Vector2.zero;
transform.position = Vector2.zero;
}
void RestartGame() {
ResetBall();
Invoke("GoBall", 1);
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.collider.CompareTag ("Player")) {
Vector2 vel;
vel.x = rb2d.velocity.x;
vel.y = (rb2d.velocity.y / 2.0f) + (coll.collider.attachedRigidbody.velocity.y / 3.0f);
rb2d.velocity = vel;
}
}
}