简体   繁体   English

如何在unity2d中使刚体移动?

[英]How to make a rigidbody move in unity2d?

I have a rigidbody2d in my unity project with circleCollider attached.我的统一项目中有一个刚体2d,附加了 circleCollider。 When the game starts, user can press left and right arrows to speed up or slow down the ball.游戏开始时,用户可以按左右箭头来加速或减速球。 But once when the ball comes to rest, even after applying huge amounts of torque, the ball is not starting to move.但是一旦球到达rest,即使施加了巨大的扭矩,球也没有开始移动。
在此处输入图像描述

The rigidbody2d values are as follows:刚体2d 值如下:
Mass 1, Linear drag 0.1, Angular drag 0.05, Gravity scale 1质量 1,线性阻力 0.1,Angular 阻力 0.05,重力刻度 1

The physics material2d values of floor are as follows: floor的物理material2d值如下:
Friction 0.4, Bounciness 0.5摩擦力 0.4,弹力 0.5

I use this line to add torque:我使用这条线来增加扭矩:

float torque = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
ballRigidBody2D.AddTorque(torque);

The torque value getting here is around 10000 - 100000到这里的扭矩值在 10000 - 100000 左右

Edit: Just now I noticed in scene view that, whenever I press left or right arrow, it suddenly rotates through an angle and then stops.编辑:刚才我在场景视图中注意到,每当我按左箭头或右箭头时,它会突然旋转一个角度然后停止。 When I release the key though, it starts to spin fast for few seconds like a top and then eventually comes to rest.但是,当我松开钥匙时,它开始像陀螺一样快速旋转几秒钟,然后最终到达 rest。 (but it doesn't move an inch while doing all these circus.) (但在做所有这些马戏团时它不会移动一英寸。)

The first thing you need to understand is that AddTorque add a rotational force and since it is a force you need the components of it so:您需要了解的第一件事是 AddTorque 添加旋转力,因为它是一种力,所以您需要它的组件:


float torque = Input.GetAxis("Horizontal");

ballRigidBody.AddTorque (torque  * rotationSpeed * Time.deltaTime);


Multiply the torque inside the AddTorque in order to make it work fine.乘以 AddTorque 内的扭矩以使其正常工作。

Then add your torque in the Fixed update that works better with physics.然后在更适用于物理的固定更新中添加您的扭矩。 Try also to add a material with zero friction to the ball that maybe can cause some problems.还可以尝试在球上添加一种摩擦力为零的材料,这可能会导致一些问题。

Make sure to have a collider on ball and floor and 2 rigidbodies确保在球和地板上有一个对撞机以及 2 个刚体

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

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