-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathshipMovement.cs
More file actions
48 lines (37 loc) · 1.11 KB
/
Copy pathshipMovement.cs
File metadata and controls
48 lines (37 loc) · 1.11 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 System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shipMovement : MonoBehaviour
{
// rigidbody, so we can apply physics to GameObject
Rigidbody2D rb;
// amount of force to apply
[SerializeField] private float movementForce;
[SerializeField] private float hValue, vValue;
// keep track of collected key
[SerializeField] private bool hazKey = false;
// Start is called before the first frame update
void Start()
{
// get reference to rigidbody on gameObject
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
GetInput();
}
private void FixedUpdate()
{
// add a force to rigidbody (moves the ship horizontally and vertically)
//rb.AddForce(new Vector2(hValue, vValue));
//
// moves ship toward/away the direction its facing
rb.AddForce(transform.up * vValue);
}
void GetInput()
{
hValue = Input.GetAxis("Horizontal") * movementForce;
vValue = Input.GetAxis("Vertical") * movementForce;
}
}