TPS射击游戏实战开发(终)

最终实现:人物控制及子弹发射


welcome

U3D自带角色控制器

Assest -> Import Package -> Characters
可以导入U3D自带的人物控制器


U3D人物控制器

人物移动及镜头移动

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using UnityEngine;
public class CameraMove : MonoBehaviour {
void Start()
{
mMoveSpeed = 5f;
isAiming = false;
cc = GetComponent<CharacterController>();
}
void Update()
{
UpdateLookAt();
//放大镜头
if (Input.GetMouseButtonDown(1))
{
if (isAiming)
{
ACamera.depth = -2;
isAiming = false;
}
else
{
ACamera.depth = 0;
isAiming = true;
}
}
}
void FixedUpdate()
{
UpdateMovement();
}
// 第一人称控制器的常量与变量
public Transform SCamera;
public Camera ACamera;
public GameObject hero;
private CharacterController cc;
private bool isAiming;
public float mMoveSpeed; // 物体移动速度
public float mMouseSensitivity = 10; // 鼠标旋转的敏感度
public float mMinimumX = -25; // 向下望的最大角度
public float mMaximumX = 45; // 向上望的最大角度
public float mMinimumY = -360; // 向左望的最大角度
public float mMaximumY = 360; // 向右望的最大角度
private Vector3 _curRotation = new Vector3(0, 0, 0); // 当前旋转角度
// 更新移动位置
private void UpdateMovement()
{
float distance = mMoveSpeed * Time.deltaTime; // 移动距离
if (Input.GetKey(KeyCode.Escape))
{
this.enabled = false;
}
if (Input.GetKey(KeyCode.W))
{
cc.Move(transform.forward * distance);
}
if (Input.GetKey(KeyCode.S))
{
cc.Move(transform.forward* -1 * distance);
}
if (Input.GetKey(KeyCode.A))
{
cc.Move(transform.right* -1 * distance);
}
if (Input.GetKey(KeyCode.D))
{
cc.Move(transform.right * distance);
}
}
// 更新摄像头指向位置
private void UpdateLookAt()
{
// 左右旋转
_curRotation.y = _curRotation.y + Input.GetAxis("Mouse X") * mMouseSensitivity;
_curRotation.y = Mathf.Clamp(_curRotation.y, mMinimumY, mMaximumY);
// 上下旋转
_curRotation.x = _curRotation.x - Input.GetAxis("Mouse Y") * mMouseSensitivity;
_curRotation.x = Mathf.Clamp(_curRotation.x, -mMinimumX, mMaximumX);
if (_curRotation.x <= -65f)
{
_curRotation.x = -65f;
}
// 设置摄像头
Vector3 rotation = transform.eulerAngles;
SCamera.localEulerAngles = new Vector3(_curRotation.x, SCamera.rotation.y, SCamera.rotation.z);
rotation.y = _curRotation.y;
transform.eulerAngles = rotation;
}
//更改速度方法
public void setSpeed(float speed)
{
mMoveSpeed = speed;
}
}

子弹射线

  1. 使用预备课中讲过的射线方法实现子弹发射
  2. 使用SendMessage这个强大的方法来实现物体Hp的减少
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    using UnityEngine;
    public class FireControl : MonoBehaviour {
    public GameObject bullet;
    public GameObject anim;
    private Animator a;
    private LineRenderer m_line;
    public Transform look;
    public Ray myRay;
    RaycastHit myHit;
    bool canFire = true;
    //射速
    int shoot_speed=0;
    //弹药
    int ammo = 30;
    void Start()
    {
    m_line = gameObject.GetComponent<LineRenderer>();
    a = anim.GetComponent<Animator>();
    }
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Q))
    {
    if (m_line.enabled)
    {
    m_line.enabled = false;
    }
    else
    m_line.enabled = true;
    }
    m_line.SetPosition(0,transform.position);
    m_line.SetPosition(1, transform.position+transform.forward);
    }
    void FixedUpdate()
    {
    if (Input.GetKeyDown(KeyCode.R))
    {
    if (ammo <= 29)
    {
    canFire = false;
    a.SetBool("Reloding", true);
    ammo = 30;
    Invoke("FireAgain",2.5f);
    }
    }
    if(canFire)
    if (Input.GetMouseButtonDown(0))
    {
    if (ammo > 0)
    {
    ammo--;
    //Instantiate(bullet, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
    myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(myRay, out myHit))
    {
    Debug.Log(myHit.collider.gameObject.name);
    myHit.collider.gameObject.SendMessage("reduce");
    }
    shoot_speed = 0;
    }
    }
    if(canFire)
    if (Input.GetMouseButton(0))
    {
    shoot_speed++;
    if (shoot_speed == 7 && ammo>0)
    {
    ammo--;
    //Instantiate(bullet, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
    myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(myRay, out myHit))
    {
    Debug.Log(myHit.collider.gameObject.name);
    myHit.collider.gameObject.SendMessage("reduce");
    }
    shoot_speed = 0;
    }
    }
    }
    void FireAgain()
    {
    canFire = true;
    a.SetBool("Reloding", false);
    }
    }


最终效果展示

结语

说是实战,其实还是个小Demo (^__^) 嘻嘻……)

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. U3D自带角色控制器
  2. 2. 人物移动及镜头移动
  3. 3. 子弹射线
  4. 4. 结语