暂定为手机游戏,那就让基本子弹自动射击(手机操控不意,让玩家只做简易核心的操作就好) ,先把上一篇制作好的子弹加在Player上,开启PlayerControl来多加几行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
private Animator anim;
private Rigidbody2D rig;
[SerializeField]float speed = 5f;
//序列化一个物件名叫buller预设为空值(null)
[SerializeField] GameObject bullet = null;
float horizontalMove;
private void Start()
{
anim = GetComponent<Animator>();
rig = GetComponent<Rigidbody2D>();
//反覆的呼叫"Attack"一秒後开始,反覆时间为一秒
InvokeRepeating("Attack", 1f, 1f);
}
private void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
Move();
}
void Move()
{
rig.velocity =
new Vector2(horizontalMove * speed, rig.velocity.y);
if (horizontalMove > 0.2f)
{
anim.SetBool("right",true);
}
if (horizontalMove < -0.2f)
{
anim.SetBool("left", true);
}
if(horizontalMove < 0.2f && horizontalMove > -0.2f)
{
anim.SetBool("right", false);
anim.SetBool("left", false);
}
}
//新增一个Attack
void Attack()
{
//生成物件(bullet(物件名称),物件位置,物件角度)
Instantiate(bullet,transform.position,Quaternion.identity);
}
}
回到unity上要记得把子弹装到Player上,点选预置物的子弹(要用project里的,场景上的还是算物件)拖曳到player程序上的bullet即可
接下来放一个敌人,从最小的开始放,放上後也记的给刚体跟碰撞器,记得改一下刚体的重力跟碰撞器的大小,最後给敌人也新增一个程序,暂定EnemyAI,开启EnemyAI程序稍微编辑一下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
//序列化一个速度 先给数值3
[SerializeField] float speed = 3f;
//给一个向右的布林值
bool right;
//开始
void Start()
{
//一个始设定向右走
right = true;
}
//持续
void Update()
{
//移动
Move();
}
//新增一个Move
void Move()
{
//如果right不是ture(或写成right==false)
if (!right)
{
//位置的转换(速度*(-1)*时间.差量时间,0,0)
transform.Translate(speed * -1 * Time.deltaTime, 0, 0);
//如果(物件的X位置小於等於0.5)
if (gameObject.transform.position.x <= 0.5)
//向右的布林值=true
right = true;
}
//如果right是ture(或写成right==true)
if (right)
{
//位置的转换(速度*(1)*时间.差量时间,0,0)
transform.Translate(speed * 1 * Time.deltaTime, 0, 0);
//如果(物件的X位置大於等於5.5)
if (gameObject.transform.position.x >=5.5)
//向右的布林值=false
right = false;
}
}
}
现在就能有个自动发射子弹的Player跟一个会左右移动的敌人,下一篇再来让他们互相伤害罗~
tags: OC 30 day 有哪些是数据类型? int,double,float,char .....
终於到最後一天啦! 很感谢 iT 铁人赛,在这一个月内,不只把我本来知道的东西透过文字或影像记录下...
linebot 结合网路爬虫 讲解完网路爬虫的实际应用後,接下来将他跟 Line chatbot 进...
SQL Server 1.一个一个TABLE去点,汇出Scripts 2.SQL Server 汇出...
上一篇介绍了Die Game,是一题判断骰子数字的题目,由於题目是中文,并且把解题丝路都跟你讲了,所...