http://pixelnest.io/tutorials/2d-game-unity/player-and-enemies/
根据官网的飞机教程
建立好scenes
给player绑定刚体代码
通过方向键控制刚体速度
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
// Use this for initialization
void Start () {
print("启动!");
}
public Vector2 speed = new Vector2(10, 10);
public Vector2 movement;
public Rigidbody2D rigidbodyComponent;
// Update is called once per frame
void Update () {
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
movement = new Vector2(
speed.x * inputX,
speed.y * inputY
);
}
void FixedUpdate()
{
if (rigidbodyComponent == null)
rigidbodyComponent = GetComponent<Rigidbody2D>();
rigidbodyComponent.velocity = movement;
}
void Awake()
{
print("我是构造函数");
}
}
Input.GetAxis是捕获键盘上的操作
Rigidbody2D.velocity 速度
http://wiki.ceeger.com/script/unityengine/classes/rigidbody2d/rigidbody2d.velocity移动脚本:
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public Vector2 movement;
public Vector2 speed = new Vector2(-2, 0);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
movement = new Vector2(
speed.x * 1,
speed.y * 0
);
}
void FixedUpdate()
{
rigidbody2D.velocity = movement;
}
}
血量脚本: