Unity学习之项目初制作

欢迎大家来到我的博客http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space

今天的内容是主角打击怪物的功能

 

一、功能及界面设置

首先需要给怪物和主角兵器添加碰撞组件 并勾选Is Trigger 属性

其次 给怪物添加刚体并去掉重力属性

主角需要具备一些必要的动画效果(如 禁止    攻击等)

二、添加相应的脚本了 废话不多说 代码如下:

主角代码: 

public class Hero : MonoBehaviour {

private NavMeshAgent agent;

public GameObject hero;

float smooth;

public static bool flag=false ;

public static  float lifetime=0;

 

void Start () {

agent =GetComponent <NavMeshAgent >();

}

void Update () {

if(Input .GetMouseButton (0)){

RaycastHit hit;

Ray ray = Camera.main.ScreenPointToRay(Input .mousePosition );

if(Physics .Raycast (ray,out hit)){

agent.SetDestination(hit.point );

}

}

}

 

void LateUpdate()

{

//调用主角动画中的攻击 禁止 走的 动画特效

if (Input.GetKey(KeyCode.Q))

{

hero.transform.animation.Play(“Skill”);

print(“aaaaaaa”);

}

else if (Input.GetKey(KeyCode.W))

{

hero.transform.animation.Play(“Attack00″);

print(“bbbbb”);

}

else if (Input.GetKey(KeyCode.E))

{

hero.transform.animation.Play(“Attack01″);

}

else  if (Input.GetKey(KeyCode.R))

{

hero.transform.animation.Play(“Attack”);

}

else if (agent.remainingDistance == 0)

{

 

OnAnimationToidle();

 

}

else

{

OnAnimationToWalk();

}

}

void OnAnimationToidle() {

hero.transform.animation.Play(“Idle”);

}

void OnAnimationToWalk()

{

hero.transform.animation.Play(“Walk”);

 

}

void OnAnimationToKill()

{

hero.transform.animation.Play(“Attack00″);

}

}

 

怪物代码:

 

 

using UnityEngine;

using System.Collections;

 

public class Monster : MonoBehaviour {

private float life = 6;

void OnTriggerEnter(Collider col)

{

if (col.CompareTag(“Sword”))

//if (col.CompareTag(“Dwarf”)) 另一种获得标签的方式

{

life–;

if(life<=0){

Destroy(this.gameObject);

Hero.lifetime++;

}

}

}

 

}

 

请继续关注我的博客

http://unity.gopedu.com/home.php?mod=space&uid=3352&do=blog&view=me&from=space

更多精彩尽在http://www.gopedu.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值