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

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

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

scratch3.0怎么创建人物打篮球动画?

scratch3.0怎么创建人物打篮球动画?scratch3.0中想要创建一个动画效果,该怎么创建呢?下面我们就来看看scratch3.0制作人物打篮球动画的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

scratch3.0制作画随机多边形的小程序?

scratch3.0制作画随机多边形的小程序?scratch3.0中想要制作一个随机化多边形的小程序,该怎么实现根据输入的边数绘制多边形呢?下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

IntelliJ IDEA 2020.1正式发布,你要的Almost都在这(推荐)

自从官方在2020-01-20发布了其2020年的Roadmap后,我便持续关注着、期待着JetBrains IntelliJ IDEA在“新10年”的首个版本的发布,感兴趣的朋友跟随小编一起看看吧
收藏 0 赞 0 分享

Visual Studio Code 1.44 解决中文代码显示乱码问题(小白图文教程)

这篇文章主要介绍了Visual Studio Code 1.44 解决中文代码显示乱码问题,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Advanced Installer 激活补丁使用教程 支持一键激活最新版本

MSI安装包制作工具 Advanced Installer 怎么免费使用?Advanced Installer激活补丁怎么能快速安装和激活呢?还有不知道的朋友一起详细了解下吧
收藏 0 赞 0 分享

scratch3.0变量和列表怎么控制花瓣数量?

scratch3.0变量和列表怎么控制花瓣数量?scratch3.0中制作花朵的时候,想要使用变量和列表来控制花瓣的数量,该怎么实现呢?下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

scratch3.0怎么使用克隆命令控制旋转花瓣数?

scratch3.0怎么使用克隆命令控制旋转花瓣数?scratch3.0绘制花瓣的时候,想要实现输入数字实现绘制旋转花瓣的数量,下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

Navicat for MySQL v15中文企业版安装激活教程(附下载)

Navicat for MySQL 15是一款第三方mysql管理工具,能够同时连接 MySQL 和 MariaDB 数据库,这里带来了Navicat for MySQL 15企业版的安装激活方法,并附注册机下载
收藏 0 赞 0 分享

scratch3.0怎么创建人物跳舞的小程序?

scratch3.0怎么创建人物跳舞的小程序?scratch3.0中想要创建一个小动画,该怎么制作一个人物跳舞的小动画呢?下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

notepad++默认单实例怎么修改? Notepad++多实例运行的技巧

notepad++默认单实例怎么修改?notepad++默认单实例,但是多实例可以同时开多个独立的Notepad窗口,下面就来看看Notepad++多实例运行的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多