简体   繁体   English

有人可以告诉我AddForce和transform.translate之间的区别吗?

[英]Can someone tell me the difference between AddForce and transform.translate?

I am trying to move a gameobject but I found these two most popular functions to move the object. 我试图移动一个游戏对象,但是发现了这两个最受欢迎的功能来移动对象。 The difference I know between these two is that there is a rigidbody needed in using AddForce but no Rigidbody needed in Transform.translate. 我知道这两者之间的区别在于,使用AddForce需要一个刚体,而在Transform.translate中则不需要刚体。 I just wanted to make sure that I am using right function for game. 我只是想确保我在游戏中使用了正确的功能。 Is there anny difference in the functionality of these two function? 这两个功能在功能上有区别吗? I am moving a cube over an infinite plane with obstacles in the way. 我正在将立方体移动到有障碍物的无限平面上。 I think rigidbody is needed ofcourse but I want to know what is the use of Transform.translate? 我认为当然需要刚体,但我想知道Transform.translate的用途是什么?

transform.translate: transform.translate:

With this method you basically teletransport the GameObject, not taking in account physics nor colliders. 使用这种方法,您基本上无需考虑物理因素或对撞机就可以远程传输GameObject。 This one is pretty expensive if you have a RigidBody attached. 如果您附有RigidBody,这是相当昂贵的。

AddForce: AddForce:

With this one you are adding force to the RigidBody of the GameObject, so all the movement will be taking physics and colliders into account. 通过这一步骤,您将为GameObject的RigidBody增添力量,因此所有运动都将考虑物理因素和对撞机。

If you need to move a RigidBody (like a Player) I recommend using MovePosition . 如果您需要移动RigidBody(例如Player),建议使用MovePosition It's more precise than AddForce and use the physics motor. 它比AddForce更精确,并使用物理马达。

EDIT: 编辑:

Example: 例:

public float movementSpeed= 5f; //for instance

void FixedUpdate()
{

    Vector3 direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0 , Input.GetAxisRaw("Vertical"));
    rigidbody.MovePosition(transform.position + direction * movementSpeed * Time.fixedDeltaTime);
}

Tweak movementSpeed to go faster or slower. 调整运动速度以加快或减慢速度。

Didn't test the code but it should work. 没有测试代码,但它应该工作。

有时您不想在游戏中使用物理,因为它需要大量的计算能力,而且还有许多其他用例,开发人员无需使用物理来移动对象或与其他对象发生碰撞是不重要。

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

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