-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallController.cs
More file actions
48 lines (42 loc) · 1.23 KB
/
BallController.cs
File metadata and controls
48 lines (42 loc) · 1.23 KB
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
43
44
45
46
47
48
using UnityEngine;
public class BallController : MonoBehaviour
{
[HideInInspector] public GameManager gameManager;
[HideInInspector] public Transform paddleTransform;
private Rigidbody2D rb;
private bool isStarted = false;
private float speed = 8f;
private Vector3 paddleOffset;
void Start()
{
rb = GetComponent<Rigidbody2D>();
paddleOffset = transform.position - paddleTransform.position;
ResetBall();
}
void Update()
{
if (!isStarted)
{
transform.position = paddleTransform.position + paddleOffset;
if (Input.GetMouseButtonDown(0))
{
isStarted = true;
rb.bodyType = RigidbodyType2D.Dynamic; // 수정된 부분
rb.velocity = new Vector2(Random.Range(-0.5f, 0.5f), 1).normalized * speed;
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(rb.velocity.magnitude < speed * 0.8f)
{
rb.velocity = rb.velocity.normalized * speed;
}
}
public void ResetBall()
{
isStarted = false;
rb.velocity = Vector2.zero;
rb.bodyType = RigidbodyType2D.Kinematic; // 수정된 부분
}
}