GunController.cs
1.62 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
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunController : MonoBehaviour
{
public float m_ScrollSpeed = 10f;
public float m_YawSpeed = 20f;
public float m_MoveSpeed = 10f;
public Gun m_Gun;
public float m_HoverRange = 0.5f;
private Vector3 m_StartPos;
private Rigidbody m_GunRigidbody;
void Start()
{
m_StartPos = m_Gun.transform.position;
m_GunRigidbody = m_Gun.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (GravityBall.Instance.GetButton() || Input.GetMouseButton(0))
{
if (m_Gun.m_CurrentState == Gun.State.Ready)
{
m_Gun.Fire();
}
else if (m_Gun.m_CurrentState == Gun.State.Empty)
{
m_Gun.Reload();
}
}
float scroll = GravityBall.Instance.GetInputSwipe("Scroll");
float yaw = -GravityBall.Instance.GetInputSwipe("Stroke");
m_GunRigidbody.AddTorque(scroll * m_ScrollSpeed * Time.deltaTime, yaw * m_YawSpeed * Time.deltaTime, 0f);
float xInput = GravityBall.Instance.GetInputAxis("Horizontal");
float zIput = GravityBall.Instance.GetInputAxis("Vertical");
m_Gun.transform.Translate(xInput * m_MoveSpeed * Time.deltaTime, 0, zIput * m_MoveSpeed * Time.deltaTime, Space.World);
if(Vector3.Distance(m_Gun.transform.position,m_StartPos) > m_HoverRange)
{
m_Gun.transform.position = m_StartPos + (m_Gun.transform.position - m_StartPos) * m_HoverRange;
}
}
}