-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerMovement.cs
40 lines (32 loc) · 1.08 KB
/
playerMovement.cs
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 System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
// reference to a rigidbody called rb
public Rigidbody rb;
// create variables so force could be adjusted
public float forwardForce = 2000f;
public float sidewardForce = 500f;
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello world");
// rb.useGravity = false;
// rb.AddForce(0, 200, 500);
}
// Update is called once per frame
// we use "Fixed"Update because dealing with physics
void FixedUpdate()
{
// add a forward force on z axis
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
// add a sideward force if key is pressed
if (Input.GetKey("d")) {
rb.AddForce(sidewardForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) {
rb.AddForce(-sidewardForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}