1.在场景生成一个空物体(Player)
2.快捷键alt+v,生成脚本
3.添加Rigidbody2d刚体,Gravity scale(重力)设置为0,拖入到other BInds脚本
4.player 移动方法:
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");//假设速度为1
var vertical = Input.GetAxis("Vertical");//假设速度为1
//斜着走速度是根号二,所有用Normalized
var direction=new Vector2(horizontal, vertical).normalized;
SelfRigidbody2D.velocity = direction*MovementSpeed;
}
5.enemy(同样alt+v生成脚本)朝player移动方法:
public partial class Enemy : ViewController
{
public float MovementSpeed = 2.0f;
private void Update()
{
var player=FindObjectOfType<Player>();
var direction = (player.transform.position - transform.position).normalized;
transform.Translate(direction*Time.deltaTime*MovementSpeed);
}
}
完成,简单的移动功能结束