Unity3D 冲锋效果、角色拖尾效果

所属分类: 软件教程 / 编程开发 阅读数: 1829
收藏 0 赞 0 分享

《魔兽世界》,本人最喜欢的网络游戏,如果你玩过战士,你一定对战士的冲锋非常熟悉,现在接触 Unity3D,因为最近用到了刀光、拖尾特效,所以就想做一个类似战士的冲锋效果,在本场景用到的拖尾效果可以查看我的另一篇文章,里面有详细的介绍,刀光效果来自 Unity3D Assets 商店,只是把原作者的例子代码整理了一下,变得非常简单实用的类。

最终效果如下:

先来搭建我们的场景,如图:

然后给角色的模型添加一个空对象,并且加上 MeshRender,并且设置好材质为 WeaponTrail,另外给这个空对象添加 WeaponTrail.cs 对象,设置好相关属性,如图:

下面的代码是修改另一篇文章的 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>
/// 运行状态
/// </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>
/// 规范化比较向量
/// </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),因为我们用这个来驱动角色移动,如图:

最后运行可以查看效果,点击地形,角色向目标点移动,并带有拖尾效果。

百度网盘下载地下:http://pan.baidu.com/s/1hqeiREO 密码: t41j

更多精彩内容其他人还在看

VS2013 ultimate许可证已过期无法输入序列号该怎么办?

VS2013 ultimate许可证已过期无法输入序列号该怎么办?看到很多网友遇到这个问题都是直接卸载从新安装的,但是又要重新设置软件,很麻烦,今天教大家一个比较简单的方法,不需要重装软件,需要的朋友可以参考下
收藏 0 赞 0 分享

DW站点中新建文件夹和修改、删除、移动文件方法图解

下面为大家分享DW站点中新建文件夹和修改、删除、移动文件方法,教程真的很不错,很基础,适合新手来学习,推荐过来,一起来学习吧
收藏 0 赞 0 分享

Eclipse怎么快速开发jni程序?

Eclipse怎么快速开发jni程序?开开发软件的时候需要配置结合java和c++的程序开发环境,才能达到两者都支持Eclipse,下面我们就来看看详细的配置方法
收藏 0 赞 0 分享

Xmanager Enterprise 5怎么破解安装?

Xmanager Enterprise 5怎么破解安装?Xmanager5是一款浏览远端X窗口系统的工具。在工作中经常使用Xmanager来登录远端的Solaris系统,下面我们来看看Xmanager5的破解安装方法
收藏 0 赞 0 分享

MyEclipse怎么新建Servlet? MyEclipse快速创建Servlet的方法

MyEclipse怎么新建Servlet?今天我们来教大家创先项目Servlet的方法和在项目中创建Servlet的方法,下面分享MyEclipse快速创建Servlet的两种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Dreamweaver创建表格和表格的编辑方法介绍

这篇教程是向脚本之家的朋友分享Dreamweaver创建表格和表格的编辑方法,教程比较基础,适合新手来学习,推荐到脚本之家,大家一起来学习吧
收藏 0 赞 0 分享

在DW中创建层和为层里插入图像

这篇教程是向脚本之家的朋友分享在DW中创建层和为层里插入图像方法,教程很基础,适合新手来学习,推荐到脚本之家,喜欢的朋友来学习吧
收藏 0 赞 0 分享

Sublime Text怎么设置文件在新标签打开?

Sublime Text怎么设置文件在新标签打开?使用Sublime Text打开文档,总是在原来的窗口打开,覆盖了原来的文件,该怎么设置文件在新窗口打开呢?下面分享详细的设置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

vs2012能编写c语言吗? VS2012编写c语言的方法

vs2012能编写c语言吗?想用Visual Studio 2012编写c语言,不知道能不能使用,今天我们来看看VS2012编写c语言的方法,下面我们来看看详细教程,需要的朋友可以参考下
收藏 0 赞 0 分享

Android studio最近任务列表该怎么清空?

Android studio最近任务列表该怎么清空?Android studio记录了很多最近打开的开项目列表,想将这个记录清空,该怎么办呢?下面我们来看看Android studio清空最近打开项目列表的方法
收藏 0 赞 0 分享
查看更多