-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMove.cs
More file actions
59 lines (48 loc) · 1.57 KB
/
PlayerMove.cs
File metadata and controls
59 lines (48 loc) · 1.57 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
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
[SerializeField]
float speed = 10f;
[SerializeField]
private float fireRate = 0.2f;
[SerializeField]
private GameObject bulletPrefab = null;
[SerializeField]
private Transform bulletPosition = null;
private Vector2 targetPosition = Vector2.zero;
private SpriteRenderer spriteRenderer = null;
private GameManager gameManager = null;
void Start()
{
gameManager = FindObjectOfType<GameManager>();
spriteRenderer = GetComponent<SpriteRenderer>();
StartCoroutine(Fire());
}
void Update()
{
if (Input.GetMouseButton(0))
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.x = Mathf.Clamp(targetPosition.x, gameManager.MinPosition.x,
gameManager.MaxPosition.x);
targetPosition.y = Mathf.Clamp(targetPosition.y, gameManager.MinPosition.y,
gameManager.MaxPosition.y);
transform.localPosition = Vector2.MoveTowards(transform.localPosition, targetPosition, speed * Time.deltaTime);
}
}
private IEnumerator Fire()
{
GameObject bullet;
if (bulletPrefab != null)
{
Debug.Log("b");
while (true)
{
bullet = Instantiate(bulletPrefab, bulletPosition);
bullet.transform.SetParent(null);
yield return new WaitForSeconds(fireRate);
}
}
}
}