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

所属分类: 软件教程 / 编程开发 阅读数: 1814
收藏 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

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

PowerDesigner16.6怎么破解?SAP PowerDesigner16.6安装破解详细图文教程

PowerDesigner是一款来自国外的数据库建模工具,最近,很多网友问小编PowerDesigner16.6怎么破解?今天脚本之家小编就给大家带来SAP PowerDesigner16.6安装破解详细图文教程,希望对大家有所帮助
收藏 0 赞 0 分享

WebStorm 2017.3最新汉化激活破解及安装教程(附汉化包+原版下载)

WebStorm 2017中文破解版是一款JavaScript开发工具,是最强大的HTML5编辑器以及最智能的JavaSscript IDE,下面就整理WebStorm 2017.3最新汉化破解教程,并附有汉化包及官方原版程序下载地址
收藏 0 赞 0 分享

PhpStorm 10.0.2怎么激活?PhpStorm 10.0.2激活破解图文教程(附注册码)

PhpStorm 10.0.2怎么激活?对于新手来说最头痛的问题还是激活和使破解的问题,今天脚本之家小编就给大家带来PhpStorm 10.0.2激活破解图文教程(附注册码),希望对大家有所帮助
收藏 0 赞 0 分享

Toad for Oracle 2017完整破解版安装教程(附注册码) 64位

Toad for oracle 2017是一款功能强大的数据库管理软件,支持敏捷数据库开发工作,是一个功能强大、结构紧凑的专业化PL/SQL开发环境,本文提供最新版的Toad for Oracle 2017详细的安装步骤,以及软件下载地址和注册码
收藏 0 赞 0 分享

Aqua Data Studio 18汉化安装破解教程(附DateStopper破解下载)

Aqua Data Studio18中文版是一套完整IDE的数据库开发工具,新版对其通用数据库管理和分析软件的一次重大升级,下面就带来了Aqua Data Studio18最新版详细安装步骤,以及破解方法,仅供参考
收藏 0 赞 0 分享

informatica powercenter 9.x安装与配置图文详细教程(适应于Windows系统)

informatica powercenter是一款使用相当广泛的数据集成平台(ETL工具),本文主要针对目前还不会安装与配置informatica powercenter 9.x程序的朋友们,来图文详细介绍informatica powercenter 9.x安装与配置教程,并
收藏 0 赞 0 分享

Myeclipse 2017 CI8汉化破解教程(附注册激活码)

MyEclipse可用于用户所有的UML, AJAX, Web, Web Services, J2EE, JSP, XML, Struts, JSF, Java Persistence, EJB,扩展数据库支持以及应用程序服务器集成需求,那么Myeclipse 2017 CI8怎
收藏 0 赞 0 分享

CoolFormat 源代码格式化工具使用帮助手册

CoolFormat源代码格式化是一款C\C++\C#\CSS\HTML\Java\JavaScript\JSON\Objective-C\PHP\SQL\XML代码格式化工具。软件可以快速多种风格格式化,并对语言进行着色。界面采用Office 2010风格,并有多种样式可以替换
收藏 0 赞 0 分享

Sublime Text 3怎么设配置浏览默认路径为localhost?

Sublime Text 3怎么设置浏览器打开就是localhost路径?Sublime Text 3编辑程序的时候,需要对代码进行预览,该怎么制定配置浏览器默认的浏览路径为localhost呢?下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

Visual Studio窗口界面显示黑色很多功能消失了怎么办?

Visual Studio窗口界面显示黑色很多功能消失了怎么办?Visual Studio打开以后,窗口变成黑色的了,新建文件的时候,发现工具栏中少了很多功能,该怎么办呢?下面我们就来看看详细的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多