实习第二天,感觉他们交了个套路,里面的东西都要自己实现。。还好之前看过一些,否则凉凉。
助教是六年前 ACM金牌。 幸福。。
接下来几天记录下每天的收获和自己做的东西吧。
做了大概整整一天吧,可以完成人物移动,人物三连击,人物释放技能并击到怪物,怪物HP掉血,并死亡。
收获最大的就是状态机的使用,和逻辑控制吧!哦对还有三连。!!!
第一天就先贴全吧,以后逐步更新逐步改过的部分
人物部分:
移动部分尝试了好多,还是这个好用! 加刚体来旋转!
三连判断时间允许范围内下一击!助教哥教的,感觉完成有些问题,但是还是实现了。不知道最后能不能通过。。
敌人部分待优化,因为敌人死亡没有出列表
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControll : MonoBehaviour
{
Animator anim;
Rigidbody rigid;
Transform trans;
private float speed = 0.5f;
private float rotationSpeed = 100.0f;
private GameManager gm;
public bool isRun;
public float timeNeed=0.5f;
private float timeAttack;
public float timeANeed = 0.3f;
public GameObject skillStartPosition;//技能释放点
public List<GameObject> enemy = new List<GameObject>();
//三连
//状态机里面的状态;
public int isThree;
//状态机里是否存在了下一帧
public int isFour;
//三连当前还剩下的秒
public float isThreeTime;
//三连下一连特效
public List<GameObject> list = new List<GameObject>();
void Start()
{
isThree = 0;
isFour = 0;
timeAttack = 0.0f;
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameManager>();
isRun = false;
trans = gameObject.GetComponent<Transform>();
rigid = gameObject.GetComponent<Rigidbody>();
anim = gameObject.GetComponent<Animator>();
anim.SetBool("toIdle", true);
}
void Update()
{
updateThreeA();
playerMove();
}
void updateThreeA()
{
isThreeTime -= Time.deltaTime;
if (isThreeTime <=0 )
{
Debug.Log(isThree + " -- " + isFour);
if (isFour == isThree)
{
isFour = 0;
isThree = 0;
isThreeTime = 0;
}
else
{
if (isFour == 1)
isThreeTime = 0.8f;
if (isFour == 2)
{
isThreeTime = 0.867f;
}
else if (isFour == 3)
{
isThreeTime = 0.5f;
}
isThree = isFour;
if(enemy.Count!=0)
gm.skillManager.ShowSkillOne(list[0],enemy[0],skillStartPosition);
}
Debug.Log(isThree);
anim.SetInteger("toThree",isThree);
}
}
void playerMove( )
{
float translation = Input.GetAxis("Vertical") * (-1.0f);
float rotation = Input.GetAxis("Horizontal");
Vector3 Rot = new Vector3(translation, 0, rotation);
if (translation != 0 || rotation != 0)
{
isRun = true;
Vector3 targetDirect = Rot;
Quaternion targetRotation = Quaternion.LookRotation(targetDirect, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigid.rotation, targetRotation, 30.0f * Time.deltaTime);
gameObject.GetComponent<Rigidbody>().MoveRotation(newRotation);
transform.Translate(Vector3.forward * speed, Space.Self);
anim.Play("Run");
}
else
{
if (isRun)
{
isRun = false;
anim.Play("Idle");
}
}
}
void Rotation(Vector3 Rot,GameObject go)
{
Vector3 targetDirect = Rot;
go.transform.LookAt(Rot);
}
//技能逻辑部分
public void DoSkill(GameObject currentSkill, int em = 0)
{
if ( enemy.Count == 0 ) return;
isRun = false;
GameObject targetEnemy = enemy[0];
Rotation(targetEnemy.transform.position, this.gameObject);
if (em == 1)
{
if (isThreeTime <= 0.2f)
{
isFour = isThree + 1;
list.Add(currentSkill);
}
else
{
if (isThree == 4)
{
isThree = 0;
isFour = 0;
}
return;
}
}
else
{
timeAttack += timeNeed;
isThree = 0;
anim.Play("Attack");
}
gm.skillManager.ShowSkillOne(currentSkill,targetEnemy,skillStartPosition);
//GameObject skillType=GameObject.Instantiate(currentSkill, skillStartPosition.transform.position, Quaternion.identity);
//Skill skill = currentSkill.GetComponent<Skill>();
//skill.SetTarget(targetEnemy);
}
//三连
public void XA(int em)
{
}
//加敌人
void OnTriggerEnter(Collider col)
{
if (col.tag == "Enemy") enemy.Add(col.gameObject);
}
void OnTriggerExit(Collider col)
{
if (col.tag == "Enemy") enemy.Remove(col.gameObject);
}
public Vector3 detsub(Vector3 st, Vector3 ed)
{
return new Vector3(ed.x - st.x, ed.y - st.y, ed.z - st.z);
}
}
怪物部分:
收获是血条与怪物的关联,
助教哥教了下画布UI竟然还可以设置为world中的画布,从而实现每个怪物都自己产生一个hp。血的数据存储在怪物中,hp条获取怪物数据,两者互相关联。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Monster : MonoBehaviour {
public int status;
public int hp;
public int fullHp = 100;
public Animator anim;
public GameObject hpSlider;
public GameObject father;
void Start () {
status = 0;
anim = gameObject.GetComponent<Animator>();
anim.Play("Stand");
hpSlider= GameObject.Instantiate(hpSlider, transform.position, Quaternion.identity);
var hpGO = hpSlider.GetComponent<HP>();
hpGO.host = gameObject;
hpGO.offsetHeight = 5.5f;
hpSlider.transform.SetParent(father.transform, false);
}
void Update () {
}
public void Die()
{
status = 4;
anim.Play("Death");
Destroy(this.gameObject,2.0f);
Destroy(hpSlider);
}
public void Damage()
{
// anim.Play("BeHit");
}
}
这个hp脚本是助教哥写的,虽然写的简单,但是思路感觉很新奇,超好用。。。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HP : MonoBehaviour {
public GameObject host = null;
public float offsetHeight = 5.5f;
public int fullHp=100;
public int hp = 100;
void Start ()
{
fullHp = host.GetComponent<Monster>().fullHp;
}
void Update () {
hp = host.GetComponent<Monster>().hp;
var pos = host.transform.position;
pos.y += offsetHeight;
transform.position = pos;
if(hp>0)
GetComponent<Slider>().value = (float)(hp*1.0 / fullHp);
}
}
接下来是技能部分了:
只是巩固了下以前的知识,重新用了下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkillManager : MonoBehaviour {
public GameManager gm;
public List<GameObject> skillnames = new List<GameObject>();
void Start()
{
gm = gameObject.GetComponent<GameManager>();
}
public void OnButtonClickSkillOne()
{
gm.LetPlayerDoSkill(skillnames[0]);
}
public void OnButtonClickSkillTwo()
{
gm.LetPlayerDoSkill(skillnames[1]);
}
public void OnButtonClickSkillThree()
{
gm.LetPlayerDoSkill(skillnames[2]);
}
public void OnButtonClickA()
{
if (gm.player.isThree == 0)
gm.LetPlayerDoSkill(skillnames[3], 1);
else if (gm.player.isThree == 1)
gm.LetPlayerDoSkill(skillnames[4], 1);
else if(gm.player.isThree == 2){
gm.LetPlayerDoSkill(skillnames[5], 1);
}
}
//实例单体技能 并飞出
public void ShowSkillOne(GameObject doSkill,GameObject targetEnemy,GameObject skillStartPosition)
{
GameObject skillType = GameObject.Instantiate(doSkill, skillStartPosition.transform.position, Quaternion.identity);
Skill skill = doSkill.GetComponent<Skill>();
skill.SetTarget(targetEnemy);
}
}
GameManger脚步:
目前好像没干什么,以后希望里面有东西吧,否则就删掉了。 怪物的部分可以不写在这里,以后优化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public PlayerControll player;
public SkillManager skillManager;
void Start () {
}
void Update () {
}
public void LetPlayerDoSkill(GameObject skillType, int em = 0)
{
player.DoSkill(skillType,em);
}
public void LetMonsterBeHit(GameObject monster, GameObject skill)
{
Monster mons = gameObject.GetComponent<Monster>();
float k = monster.GetComponent<Monster>().hp;
monster.GetComponent<Monster>().hp -= skill.GetComponent<Skill>().damage;
if (monster.GetComponent<Monster>().hp <= 0)
{
monster.GetComponent<Monster>().Die();
}
else
{
monster.GetComponent<Monster>().Damage();
}
}
}
摄像机:2.5D锁定跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControll : MonoBehaviour {
public GameObject player;
void Start()
{
}
void Update()
{
this.transform.position = (new Vector3(player.transform.position.x + 6, player.transform.position.y+ 10, player.transform.position.z - 4));
}
}