经过实践了二维平面上物体移动的场景,回顾一下,温故而知新。
有以下几类方式。
1.通过键盘方式控制
Input.GetKey(KeyCode.W)
this.gameObject.transform.Translate(Vector3.up*Time.deltaTime);
2.用开发环境自带设置,Input.GetAxis()
float horizontal=Input.GetAxis(“Horizontal”);
float vertical=Input.GetAxis(“Vertical”);
Player.transform.Translate(Vector3.up * vertical * m_speed * Time.deltaTime);
Player.transform.Translate(Vector3.right * horizontal * m_speed * Time.deltaTime);
3.使用推动力
speed = (new Vector2(2,1))*Power;
GetComponent().AddForce(speed);
4. 赋予一个刚体初速度
pointbird.GetComponent().AddForce(velocity,ForceMode2D.Impulse);
5.直接指定物体位置
this.transform.position = new Vector2(18.5f, 0);
这里面1、2两种是通过位移,3、4两种是利用刚体,但是有一个问题是当物体是刚体属性时,也能使用位移就是穿过其他物体或者说当其他物体能够有间隙移动时,从其他物体的间隙中通过。第5种可以当做顺时移动至某一位置。
Unity二维平面上物体的移动(十五)
最新推荐文章于 2024-12-16 09:07:33 发布