您的位置:首页 > 移动开发 > Unity3D

unity学习笔记之物体移动

2016-10-22 00:05 621 查看

让物体运动的三种方法

(1)Transform.Translate 平移

void Start () {
}

void Update () {
gameObject.transform.Translate (-5 * Time.deltaTime, 0f, 0f, Space.Self);
}


(2)Rigidbody.velocity 移动

float upspeed = 9f;
float downspeed = -5f;

Vector2 up;
Vector2 down;
Rigidbody2D rbody;

bool isground = false;
bool isduck = false;

void Start () {
up = new Vector2 (0f,upspeed);
down = new Vector2 (0f, downspeed);
rbody = gameObject.GetComponent<Rigidbody2D> ();
}

void Update () {
}
void FixedUpdate(){
if (Input.GetKeyDown (KeyCode.W) && isground)
rbody.velocity = up;
}


(3)Rigidbody.AddForce 移动

void FixedUpdate() {
rigidbody.AddForce(0, 10, 0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity