Unity3D 沖鋒效果、角色拖尾效果

《魔獸世界》,本人最喜歡的網(wǎng)絡(luò)游戲,如果你玩過戰(zhàn)士,你一定對戰(zhàn)士的沖鋒非常熟悉,現(xiàn)在接觸 Unity3D,因為最近用到了刀光、拖尾特效,所以就想做一個類似戰(zhàn)士的沖鋒效果,在本場景用到的拖尾效果可以查看我的另一篇文章,里面有詳細的介紹,刀光效果來自 Unity3D Assets 商店,只是把原作者的例子代碼整理了一下,變得非常簡單實用的類。
最終效果如下:
先來搭建我們的場景,如圖:
然后給角色的模型添加一個空對象,并且加上 MeshRender,并且設(shè)置好材質(zhì)為 WeaponTrail,另外給這個空對象添加 WeaponTrail.cs 對象,設(shè)置好相關(guān)屬性,如圖:
下面的代碼是修改另一篇文章的 TrailsBladeMaster.cs 類,新的代碼如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("PocketRPG/Blade Master")]
public class TrailsBladeMaster : MonoBehaviour
{
/// <summary>
/// 拖尾效果
/// </summary>
public WeaponTrail weaponSwipe;
public AnimationClip idleClip;
public AnimationClip runClip;
/// <summary>
/// 移動速度
/// </summary>
public float speed = 20.0f;
public Camera mainCamera;
private Animation animation;
protected TrailsAnimationController animationController;
protected CharacterController characterController;
/// <summary>
/// 運行狀態(tài)
/// </summary>
private bool isMoving = false;
/// <summary>
/// 目標位置
/// </summary>
private Vector3 targetPosition;
/// <summary>
/// 移動向量
/// </summary>
private Vector3 moveDirection;
protected void Awake ()
{
this.animation = this.GetComponent<Animation> ();
this.animationController = this.GetComponent<TrailsAnimationController> ();
this.characterController = this.GetComponent<CharacterController> ();
this.animation.CrossFade (this.idleClip.name);
}
protected void Start ()
{
if (this.weaponSwipe != null) this.animationController.AddTrail (this.weaponSwipe);
}
protected void Update ()
{
if (!this.isMoving && Input.GetMouseButtonDown(0))
{
this.targetPosition = this.GetWorldPosition();
if(this.targetPosition != Vector3.zero)
{
this.isMoving = true;
this.moveDirection = (this.targetPosition - this.transform.position).normalized * this.speed;
this.transform.rotation = Quaternion.LookRotation(new Vector3(this.moveDirection.x, 0f, this.moveDirection.z));
this.animation.CrossFade(this.runClip.name);
if(this.weaponSwipe != null) this.weaponSwipe.StartTrail(1f, 0f);
}
}
if (this.isMoving)
{
if(!this.IsArrivePosition())
{
this.characterController.Move(this.moveDirection * Time.deltaTime);
}
else
{
this.animation.CrossFade(this.idleClip.name);
if(this.weaponSwipe != null) this.weaponSwipe.ClearTrail();
this.transform.position = this.targetPosition;
this.isMoving = false;
}
}
}
/// <summary>
/// 驗證是否到達目標地點
/// </summary>
/// <returns><c>true</c> if this instance is arrive position; otherwise, <c>false</c>.</returns>
private bool IsArrivePosition()
{
Vector3 currentDirection = (this.targetPosition - this.transform.position).normalized;
if (this.CalculateNormalized (currentDirection) == this.CalculateNormalized (this.moveDirection) * -1)
{
return true;
}
return false;
}
/// <summary>
/// 規(guī)范化比較向量
/// </summary>
/// <returns>The normalized.</returns>
/// <param name="direction">Direction.</param>
private Vector3 CalculateNormalized(Vector3 direction)
{
Vector3 value = Vector3.zero;
value.x = direction.x > 0 ? 1 : -1;
value.z = direction.z > 0 ? 1 : -1;
return value;
}
/// <summary>
/// 獲取世界位置
/// </summary>
/// <returns>The world position.</returns>
private Vector3 GetWorldPosition()
{
Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if(Physics.Raycast(ray, out raycastHit))
{
if(raycastHit.collider.gameObject.name == "Terrain")
{
return raycastHit.point;
}
}
return Vector3.zero;
}
}
最后給角色對象掛載 TrailsAnimationController.cs 組件以及 TrailsBladeMaster.cs 組件,同時還需要添加一個角色控制器(CharacterController),因為我們用這個來驅(qū)動角色移動,如圖:
最后運行可以查看效果,點擊地形,角色向目標點移動,并帶有拖尾效果。
百度網(wǎng)盤下載地下:http://pan.baidu.com/s/1hqeiREO 密碼: t41j
相關(guān)文章
- 這篇文章主要介紹了Unity3D 使用 WWW 加載場景并顯示進度條,并附上相關(guān)代碼,有需要的朋友參考下2014-10-20
Unity3D 場景導(dǎo)出成 XML 并解析還原場景
這篇文章主要介紹了Unity3D 場景導(dǎo)出成 XML 并解析還原場景,中間部分代碼取自互聯(lián)網(wǎng),并進行了修改,有需要的朋友可以參考下2014-10-20Unity3D游戲開發(fā) 宣雨松著 PDF掃描版[27MB]
《Unity3D游戲開發(fā)》通過實例詳細介紹了如何使用Unity 進行游戲開發(fā),書中先簡要介紹了Unity 環(huán)境搭建、編輯器和GUI 游戲界面相關(guān)的知識,接著介紹了如何使用C# 和JavaScri2014-05-10- 這篇文章主要介紹了Unity3D中自動調(diào)用的方法總結(jié),需要的朋友可以參考下2014-04-24
- 這篇文章主要介紹了Unity3D 實現(xiàn)怪物巡邏、按路線行走操作,由于之前沒什么經(jīng)驗,就只能按照自己的想法很愚笨的實現(xiàn)的,也算拋磚引玉,如果讀者知道如何更簡單的實現(xiàn)方式,2014-10-20