Unity3D游戏开发之角色冲锋效果、角色拖尾效果
- 格式:doc
- 大小:495.50 KB
- 文档页数:6
《魔兽世界》,本人最喜欢的网络游戏,如果你玩过战士,你一定对战士的冲锋非常熟悉,现在接触 Unity3D,因为最近用到了刀光、拖尾特效,所以就想做一个类似战士的冲锋效果,在本场景用到的拖尾效果可以查看我的另一篇文章,里面有详细的介绍,刀光效果来自 Unity3D Assets 商店,只是把原作者的例子代码整理了一下,变得非常简单实用的类。
最终效果如下:
先来搭建我们的场景,如图:
然后给角色的模型添加一个空对象,并且加上 MeshRender,并且设置好材质为 WeaponTrail,另外给这个空对象添加 WeaponTrail.cs 对象,设置好相关属性,如图:文章来自【狗刨学习网】
ing UnityEngine;
ing System.Collections;
ing System.Collections.Generic;
4.
5.[AddComponentMenu("PocketRPG/Blade Master")]
6.public class TrailsBladeMaster : MonoBehaviour
7.{
8. ///
9. /// 拖尾效果
10. ///
11. public WeaponTrail weaponSwipe;
12.
13. public AnimationClip idleClip;
14. public AnimationClip runClip;
15.
16. ///
17. /// 移动速度
18. ///
19. public float speed = 20.0f;
20.
21. public Camera mainCamera;
22.
23. private Animation animation;
24.
25. protected TrailsAnimationController animationController;
26. protected CharacterController characterController;
27.
28. ///
29. /// 运行状态
30. ///
31. private bool isMoving = false;
32.
33. ///
34. /// 目标位置
35. ///
36. private Vector3 targetPosition;
37.
38. ///
39. /// 移动向量
40. ///
41. private Vector3 moveDirection;
42.
43. protected void Awake ()
44. {
45. this.animation = this.GetComponent ();
46. this.animationController = this.GetComponent[tr]
();
47. this.characterController = this.GetComponent ();
48.
49. this.animation.CrossFade ();
50. }
51. protected void Start ()
52. {
53.if (this.weaponSwipe != null)
this.animationController.AddTrail (this.weaponSwipe);
54. }
55.
56. protected void Update ()
57. {
58.if (!this.isMoving &&
Input.GetMouseButtonDown(0))
59. {
60. this.targetPosition =
this.GetWorldPosition();
61. if(this.targetPosition != Vector3.zero)
62. {
63. this.isMoving = true;
64. this.moveDirection =
(this.targetPosition - this.transform.position).normalized * this.speed;
65. this.transform.rotation =
Quaternion.LookRotation(new Vector3(this.moveDirection.x, 0f, this.moveDirection.z));
66. this.animation.CrossFade(this.r
);
67. if(this.weaponSwipe != null)
this.weaponSwipe.StartTrail(1f, 0f);
68. }
69. }
70. if (this.isMoving)
71. {
72. if(!this.IsArrivePosition())
73. {
74. this.characterController.Move(t
his.moveDirection * Time.deltaTime);
75. }
76. else
77. {
78. this.animation.CrossFade(this.i
);
79. if(this.weaponSwipe != null)
this.weaponSwipe.ClearTrail();
80. this.transform.position =
this.targetPosition;
81. this.isMoving = false;
82. }
83. }
84. }
85.
86. ///
87. /// 验证是否到达目标地点
88. ///
89. /// true if this instance is arrive position; otherwise,
false.
90. private bool IsArrivePosition()
91. {
92. Vector3 currentDirection = (this.targetPosition -
this.transform.position).normalized;
93. if (this.CalculateNormalized (currentDirection)
== this.CalculateNormalized (this.moveDirection) * -1)
94. {
95. return true;
96. }
97. return false;
98. }
99.
100. ///
101. /// 规范化比较向量
102. ///
103. /// The normalized.
104. /// Direction.
105. private Vector3 CalculateNormalized(Vector3 direction) 106. {
107. Vector3 value = Vector3.zero;
108. value.x = direction.x > 0 ? 1 : -1;
109. value.z = direction.z > 0 ? 1 : -1;
110. return value;
111. }
112.
113. ///
114. /// 获取世界位置
115. ///
116. /// The world position.
117. private Vector3 GetWorldPosition()
118. {
119.Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
120. RaycastHit raycastHit;
121. if(Physics.Raycast(ray, out raycastHit))
122. {
123. if( == "Terrain")
124. {
125. return raycastHit.point;
126. }
127. }
128. return Vector3.zero;
129. }
130.}
复制代码
最后给角色对象挂载 TrailsAnimationController.cs 组件以及TrailsBladeMaster.cs 组件,同时还需要添加一个角色控制(CharacterController),因为我们用这个来驱动角色移动,如图:
最后运行可以查看效果,点击地形,角色向目标点移动,并带有拖尾效果。