Unity3D 冲锋效果.角色拖尾效果
《魔兽世界》,本人最喜欢的网络游戏,如果你玩过战士,你一定对战士的冲锋非常熟悉,现在接触 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.GetComponentAnimation ();
this.animationController = this.GetComponentTrailsAnimationController ();
this.characterController = this.GetComponentCharacterController ();
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
/// returnsctrue/c if this instance is arrive position; otherwise, cfalse/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
/// returnsThe normalized./returns
/// param name=directionDirection./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
/// returnsThe 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),因为我们用这个来驱动角色移动,如图:
最后运行可以查看效果,点击地形,角色向目标点移动,并带有拖尾效果。
百度网盘下载地下: 密码: t41j
- 1佛山pc扩散板厂(扩散板生产线)
- 2湛江扩散板材厂家电话地址(湛江板房厂)
- 3亚克力导光板灯饰(亚克力导光板灯饰效果图)
- 4液晶电视均光板导光板(液晶电视均光板导光板区别)
- 5热压导光板项目(导光板热压机操作规程)
- 6tv扩散板使用的原材料(扩散板是什么材料做的?)
- 7包含屏幕显示器扩散板材厂家的词条
- 8如何选择入宅和搬家吉日?寻找最佳时机助您美满生活
- 9bilibili稍后播放怎么不自动(为什么bilibili播放自动暂停)
- 10电磁波扩散板的作用是什么(电磁波扩散板的作用是什么呢)
- 11扩散板涂覆工艺有哪些(扩散板用什么材料好)
- 12东阳三角扩散板(东阳三角扩散板专卖店)
- 13显示器导光板厂商价格多少(液晶屏幕导光板)
- 14揭秘古董家具行业:潜在投资与文化价值的无限魅力
- 152014年家居行业薪酬调查:行业人才稀缺,薪酬水平稳步上升
- 16石家庄搬家公司推荐,专业服务,确保顺利搬迁
- 17搬家吉日选择指南,几点最佳?
- 18民航湖南安全监督管理局待遇怎么?
- 19花小猪行程自动分享(花小猪行程记录可以删吗)
- 20隐藏分多少算高(隐藏分多少合适)
- 21听音室扩散板怎样设计(听音室扩散板安装位置图)
- 22家具行业营销策划:如何打造成功的营销计划
- 23梦幻碎甲刃多少钱(梦幻西游碎甲刃对任务怪有用吗?)
- 24四川防刮花pc扩散板哪家便宜的简单介绍
- 25纳米UGR扩散板(纳米光扩散剂)
- 26极品乐音的扩散板作用(音响扩散板的分类)
- 27珠海激光导光板与印刷(珠海激光导光板行业)
- 28用竹子做扩散板好吗视频的简单介绍
- 29直下式扩散板均匀度的改善(扩散板位置)
- 30东莞亚克力扩散板哪里买(东莞市亚克力制品)