unity3d射箭实现
⽤ iTween 抛物线 移动箭⽀
添加射箭动画事件
⽤ 动画编辑器 添加事件
你选中FBX⽂件⾥的clip动画⽂件
(⽐如你这个blade_girl@Attack00.fbx⾥⾯的动画⽂件是那个Attack00)
选中以后按CTRL+D 这样就把clip提取出来了 这才是可编辑的
把提取出来的clip拖到⾓⾊上 Animation组件上
选中⾓⾊
打开 菜单栏->Animation窗⼝ ->选择Preview下⾯按钮->选择动画⽚段->双击时间栏下⾯,弹出添加动画事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 射箭代码,放在要射箭⾓⾊⾝上
/// </summary>
public class shootArrow : MonoBehaviour
{
public GameObject arrowPrefab;//从⾯板⼿动赋值
public Transform arrowShootPos;//从⾯板⼿动赋值
public Transform enemyT;//从⾯板⼿动赋值
public float arrowMoveSpeed = 10f;
public float attackSpeed = 1f;
public KeyCode keyCode =KeyCode.Q;
UnitComponent uc;
bool isInit = false;
void Init()
{
if (false == isInit)
{
isInit = true;
uc = GetComponent<UnitComponent>();
}
}
//攻击动画关键点射出箭
public void attackAnimtaionShootEvent()
{
shoot(arrowPrefab, arrowShootPos, enemyT.position, arrowMoveSpeed);
}
//攻击动画关键点射箭1轮结束
unity3d animation
public void attackAnimtaionOverEvent()
{
uc.animControl.SetState(AnimationType.idle);
}
void Update()
{
Init();
if (Input.GetKeyDown(keyCode))
{
uc.anim["attack"].speed = this.attackSpeed;
uc.animControl.SetState(AnimationType.attack);
uc.animControl.SetState(AnimationType.attack);
}
}
/// <summary>
/
//
/// </summary>
/// <param name="arrowPrefab">箭⽀预设物体</param>
/// <param name="arrowShootPos">箭⽀发射位置和⾓度</param>
/// <param name="enemyPos">⽬标位置</param>
/// <param name="speed">速度</param>
void shoot(GameObject arrowPrefab, Transform arrowShootPos,Vector3 enemyPos,float speed)
{
//抛物线
Vector3[] paths = new Vector3[3];
paths[0] = arrowShootPos.position;//第⼀个点:起始位置.
Vector3 dir = (enemyPos - arrowShootPos.position).normalized;
float distance = Mathf.Abs(Vector3.Distance(new Vector3(enemyPos.x, arrowShootPos.position.y, enemyPos.z), arrowShootPos.position));        //第⼆个点:⾓⾊和⽬标的中间
paths[1] = arrowShootPos.position + dir * (distance / 2f);//第⼆个点:起始位置前⽅⼏⽶处上⽅⼏⽶的坐标.
paths[1].y = enemyPos.y + 0.25f;
paths[2] = enemyPos;//终点,⽬标
GameObject arrowObj = GameObject.Instantiate(arrowPrefab, arrowShootPos.position, ation);
iTween.MoveTo(arrowObj, iTween.Hash(
"speed", speed,
"path", paths,
"looktarget", enemyPos
));
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
/// <summary>
/// 单位组件,这个类获取组件⽤的
/
// </summary>
public class UnitComponent : MonoBehaviour {
public Animation anim;
public NavMeshAgent agent;
public AnimationControl animControl;
// Use this for initialization
void Start ()
{
GetAllComponents();
}
private void GetAllComponents()
{
anim = getComponentEx<Animation>("Animation");
agent = getComponentEx<NavMeshAgent>("NavMeshAgent");
animControl = getComponentEx<AnimationControl>("AnimationControl");
}
public T getComponentEx<T>(string ComponentName)
{
T t2 =GetComponent<T>();
if(t2 == null)
{
Debug.LogError(gameObject.name + "ge Component" + ComponentName + "false");            return default(T);
}
return t2;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 控制动画播放的
/// </summary>
public class AnimationControl : MonoBehaviour
{
public AnimationType animationType = AnimationType.idle;
UnitComponent uc;
bool isInit = false;
void Init()
{
if (false == isInit)
{
isInit = true;
uc = GetComponent<UnitComponent>();
uc.anim["free"].wrapMode = WrapMode.Loop;
uc.anim["idle"].wrapMode = WrapMode.Loop;
uc.anim["idle"].wrapMode = WrapMode.Loop;            uc.anim["walk"].wrapMode = WrapMode.Loop;            uc.anim["walk"].wrapMode = WrapMode.Loop;            uc.anim["attack"].wrapMode = WrapMode.Once;            uc.anim["death"].wrapMode = WrapMode.Once;            uc.anim["skill"].wrapMode = WrapMode.Once;        }
}
public void SetState(AnimationType animationType)    {
this.animationType= animationType;
Init();
switch (animationType)
{
case AnimationType.free:
uc.anim.CrossFade("free");
break;
case AnimationType.idle:
uc.anim.CrossFade("idle");
break;
case AnimationType.walk:
uc.anim.CrossFade("walk");
break;
case AnimationType.run:
uc.anim.CrossFade("run");
break;
case AnimationType.attack:
uc.anim.CrossFade("attack");
break;
case AnimationType.skill:
uc.anim.CrossFade("skill");
break;
case AnimationType.death:
uc.anim.CrossFade("death");
break;
}
}
}
public enum AnimationType
{
free,
idle,
walk,
run,
attack,
death,
skill,
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。