-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
38 lines (33 loc) · 1.39 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Asteroids
{
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private CameraManager cameraManager;
private PlayerData playerData;
private Rigidbody rb;
private void Start()
{
playerData = GetComponent<PlayerData>();
rb = GetComponent<Rigidbody>();
//Set position in front of camera
transform.position = cameraManager.MainCamera.transform.position + cameraManager.MainCamera.transform.forward * playerData.PlayerDistanceFromCamera;
}
public void MoveShip(Vector2 movementDirection)
{
if (rb.velocity.sqrMagnitude > 0)
{
if (Vector3.Dot(transform.up, rb.velocity.normalized) < -0.6f)
{
rb.AddForce(transform.up * Mathf.Max(0, movementDirection.y) * Time.deltaTime * playerData.Speed * playerData.SpeedTurn);
}
else rb.AddForce(transform.up * Mathf.Max(0, movementDirection.y) * Time.deltaTime * playerData.Speed);
}
else rb.AddForce(transform.up * Mathf.Max(0, movementDirection.y) * Time.deltaTime * playerData.Speed);
transform.rotation *= Quaternion.Euler(0, 0, movementDirection.x * -playerData.RotSpeed * Time.deltaTime);
}
}
}