简体   繁体   English

使用MovePosition的Unity 3D刚体2D运动

[英]Unity 3D Rigidbody 2D movement using MovePosition

Working on 2D mode, I have a script attached to a Sprite. 在2D模式下,我将脚本附加到Sprite。 The Update() part is as follow: Update()部分如下:

void Update () {
  if (Input.GetKeyDown ("left")) {
    cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position - speedX);
  } else if (Input.GetKeyDown ("right")) {
    cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position + speedX);
  } else if (Input.GetKeyDown ("up")) {
    cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position + speedY);
  } else if (Input.GetKeyDown ("down")) {
    cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position - speedY);
  }
}

where cursor is a Prefab linked in Inspector. 其中cursor是在Inspector中链接的Prefab。 The cursor prefab has only 2 components, Sprite Renderer (to define the image), and the Rigidbody 2D, which setting is as follow: cursor预制只有2个组件,Sprite Renderer(用于定义图像)和Rigidbody 2D,其设置如下:

  • Mass = 1 质量= 1
  • Linear Drag = 0 线性阻力= 0
  • Angular Drag = 0 角阻力= 0
  • Gravity Scale = 0 重力比例= 0
  • Fixed Angle = true 固定角度=真
  • Is Kinematic = false 运动学=假
  • Interpolate = None 插值=无
  • Sleeping Mode = Start Awake 睡眠模式=开始唤醒
  • Collision Detection = Discrete 碰撞检测=离散

But when I press the arrow keys, the sprite showing on screen does not move. 但是,当我按箭头键时,屏幕上显示的精灵不会移动。 What did I miss? 我错过了什么?

Tried to add Debug.Log() inside the if case, it really enters the case. 试图在if情况下添加Debug.Log() ,它确实进入了情况。 And no error occurred. 并没有发生错误。

Note: speedX = new Vector2(1,0); 注意: speedX = new Vector2(1,0); and speedY = new Vector2(0,1); speedY = new Vector2(0,1);

You're trying to move an object that doesn't exist from the game's perspective. 从游戏的角度来看,您正在尝试移动一个不存在的对象。 Assigning cursor a prefab by dragging it from the assets library, is like assigning a blueprint. 通过从资产库中拖动cursorcursor分配预制件,就像分配蓝图一样。 Telling the prefab to move is like yelling at a car's blueprint to accelerate :) 告诉预制件移动就像喊着汽车的蓝图以加速:)

There are two general solutions. 有两种通用解决方案。

1) Save a reference 1)保存参考

Instantiate the prefab and save the resulting reference to manipulate your new object. 实例化预制件并保存生成的引用以操纵新对象。 You can directly cast it to Rigidbody2D if you're mainly interested in using the rigidbody. 如果您主要对使用刚体感兴趣,可以将其直接转换为Rigidbody2D The cast will only work if your prefab variable is of the same type, otherwise it will raise an exception. 仅当您的预制变量为相同类型时,才能进行强制转换,否则将引发异常。 This way will ensure that your prefab always contains a Rigidbody2D or you can't even assign it in the editor. 这样可以确保您的预制件始终包含Rigidbody2D或者甚至不能在编辑器中分配它。

public Rigidbody2D cursorPrefab; // your prefab assigned by the Unity Editor

Rigidbody2D cursorClone = (Rigidbody2D) Instantiate(cursorPrefab);
cursorClone.MovePosition (cursorClone.position - speedX);

2) Assign the script to the prefab 2)将脚本分配给预制

Depending on your game and what you want to achieve, you can also add a script directly to the prefab. 根据您的游戏和想要实现的目标,您还可以将脚本直接添加到预制中。 This way every instance of it would just control itself. 这样,它的每个实例都可以控制自己。

void Update () {
  if (Input.GetKeyDown ("left")) {
    rigidbody2D.MovePosition (rigidbody2D.position - speedX);
  } else { //...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM