-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThirdPersonShooterController.cs
67 lines (60 loc) · 2.58 KB
/
ThirdPersonShooterController.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using StarterAssets;
using UnityEngine.InputSystem;
public class ThirdPersonShooterController : MonoBehaviour
{
[SerializeField] private CinemachineVirtualCamera aimVirtualCamera;
[SerializeField] private float normalSensitivity;
[SerializeField] private float aimSensitivity;
[SerializeField] private LayerMask aimColliderLayerMask = new LayerMask();
[SerializeField] private Transform debugTransform;
[SerializeField] private Transform pfBulletProjectile;
[SerializeField] private Transform spawnBulletPosition;
private ThirdPersonController thirdPersonController;
private StarterAssetsInputs starterAssetsInputs;
private void Awake()
{
thirdPersonController = GetComponent<ThirdPersonController>();
starterAssetsInputs = GetComponent<StarterAssetsInputs>();
}
// Update is called once per frame
void Update()
{
Vector3 mouseWorldPosiiton = Vector3.zero;
Vector2 screenCenterPoint = new Vector2(Screen.width / 2f, Screen.height / 2f);
Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask))
{
debugTransform.position = raycastHit.point;
mouseWorldPosiiton = raycastHit.point;
}
else
{
debugTransform.position = ray.GetPoint(20f);
}
if (starterAssetsInputs.aim)
{
aimVirtualCamera.gameObject.SetActive(true);
thirdPersonController.SetSensitivity(aimSensitivity);
thirdPersonController.SetRotateOnMove(false);
Vector3 worldAimTarget = mouseWorldPosiiton;
worldAimTarget.y = transform.position.y;
Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 20f);
} else
{
aimVirtualCamera.gameObject.SetActive(false);
thirdPersonController.SetSensitivity(normalSensitivity);
thirdPersonController.SetRotateOnMove(true);
}
if(starterAssetsInputs.shoot)
{
Vector3 aimDir = (mouseWorldPosiiton - spawnBulletPosition.position).normalized;
Instantiate(pfBulletProjectile, spawnBulletPosition.position, Quaternion.LookRotation(aimDir, Vector3.up));
starterAssetsInputs.shoot = false;
}
}
}