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

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

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

MySQL图形工具 MySQL GUI Tools的安装使用方法

MySQL GUI Tools 是官方的工具,一个可视化界面的MySQL数据库管理控制台,提供了四个非常好用的图形化应用程序,方便数据库管理和数据查询
收藏 0 赞 0 分享

Navicat Premium 10.0.5中文版 oracle连接字符集报错解决办法

针对Navicat Premium 10.X 中文版解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用Navicat for Oracle工具连接oracle的图文教程

今天上网的时候偶然发现了一款oracle的客户端的图形化管理和开发工具,当看到这个界面的时候,感觉很舒服,便上网搜了一下这个工具,看百度百科之后感觉很出乎我的意料,这个产品对于许多的数据库竟都有支持
收藏 0 赞 0 分享

Windows7旗舰版32位Oracle10g的安装和卸载教程

今天下午在Win7上安装了一下oracle10g,虽然准备了挺多资料,但是还是出现了错误,安装了两次,又卸载了一次,花了将近四个小时才把Oracel10g装到了Win7上,在这个过程遇到一些问题和我遇到的错误并写成了文档,现在和大家分享一下吧
收藏 0 赞 0 分享

APMServ Apache启动失败故障排除方法

相信现在有很多黑友在用APMServ为本地搭建Web服务器,用来测试一些整站系统等等。在安装的时候有时候会遇到Apache服务启动失败的问题。我以个人的经验总结了一下,现在分享给大家,希望对各位有所帮助
收藏 0 赞 0 分享

MyEclipse 8.0正式版发布(下载以及安装+注册码+破解+升级)

MyEclipse Enterprise Workbench(企业级工作平台) 8.0版发布了! MyEclipse是对Eclipse IDE的扩展,利用它我们可以在数据库和J2EE的开发、发布,以及应用程序服务器的整合方面极大的提高工作效率
收藏 0 赞 0 分享

MyEclipse中配置struts.xml自动提示的方法

怎样设置才能在MyEclipse中让struts.xml文件出现自动提示的功能,我以为这是很简单的事,只要将该struts.xml文件所对应的dtd文件关联上就行了,但是那时我也是第一次整这个配置,有些问题并不是很明白
收藏 0 赞 0 分享

MyEclipse10和Tomcat7运行Web页面的配置方法

这篇文章主要介绍如何在MyEclipse10中,结合Tomcat在浏览器中看到写的Web页面的配置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

MyEclipse项目中的构建路径和类路径lib的问题分享

对于用构建路径的方式将jar导进来,和将jar包放入到lib下结果是一样的但是两者有一些的不同时需要注意的
收藏 0 赞 0 分享

RubyMine编辑器中安装CoffeeScript和CoffeeScriptRedux的方法

本文主要讲解在RubyMine编辑器中安装CoffeeScript和CoffeeScriptRedux,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多