-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPlayer.cs
More file actions
40 lines (31 loc) · 886 Bytes
/
Player.cs
File metadata and controls
40 lines (31 loc) · 886 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
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject go;
public Transform tform;
public Rigidbody2D rb;
public float speed = 24f;
int hitCount = 0;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
go = gameObject;
tform = transform;
}
void FixedUpdate()
{
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),0);
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
var velocityChange = (targetVelocity - (Vector3)rb.velocity);
rb.AddForce(velocityChange, ForceMode2D.Force);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Bullet"))
{
hitCount++;
Debug.Log("Hit " + hitCount + " times!");
}
}
}