僵尸的行走
我们的项目是重置并在基础上创新植物大战僵尸。经过一段时间的学习后,开始了我们的项目。 这次我的任务是完成僵尸的行走
1:导入僵尸素材并形成动画
在网上寻找僵尸行走的素材,加入unity的assets中 选中所有图片,拖入Hierarchy中形成动画
2:调整坐标,使得僵尸和植物坐标重合时
出现如图身位,即僵尸啃食植物时,使僵尸的坐标等于植物的坐标,便于后续实现啃食植物功能
3:僵尸移动脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zombie : MonoBehaviour
{
private Animator animator;
private Grid currGird;
private float speed = 6;
void Start()
{
animator = GetComponentInChildren<Animator>();
int rangeWalk = Random.Range(1, 4);
switch (rangeWalk)
{
case 1:
animator.Play("Zombie_Walk1");
break;
case 2:
animator.Play("Zombie_Walk2");
break;
case 3:
animator.Play("Zombie_Walk3");
break;
}
GetGirdByVerticalNum(0);
}
void Update()
{
Move();
}
private void Move()
{
if (currGird == null) return;
transform.Translate(new Vector2(-1.33f, 0) * (Time.deltaTime / 1) / speed);
}
private void GetGirdByVerticalNum(int verticalNum)
{
currGird = GridManager.Instance.GetGridByVerticalNum(verticalNum);
transform.position = new Vector3(transform.position.x, currGird.Position.y);
}
}
public Grid GetGridByVerticalNum(int verticalNum)
{
for (int i = 0; i < GirdList.Count; i++)
{
if (GirdList[i].Point == new Vector2(8, verticalNum))
{
Debug.Log(i);
Debug.Log(GirdList[i].Point);
return GirdList[i];
}
}
return null;
}
}
|