英文:
In Unity3d how can I move a gameobject with mouse while interacting with physics engine?
问题
我想开发一个3D游戏。一个“桌上曲棍球”模拟器。我想用鼠标来控制球拍,但同时我也想使用物理引擎来处理与冰球和桌子边缘的碰撞。
我尝试了以下方法:
- 如果我使用一个运动学球拍,我失去了物理引擎处理的属性。
- 如果我使用静态对象也是一样的。
- 如果我使用动态刚体,我无法平稳地控制球拍。
如何最好地处理这种情况?
谢谢。
英文:
I would like to develop a 3d game. An "air hockey" simulator. I would like to control the paddle with the mouse but at the same time, I would like to use the physics engine to handle collisions with the puck and the borders of the table.
I tried the following:
- If I use a Kinematic paddle I lose the property to handle the physics by the engine.
- If I use static object the same.
- If I use a dynamic rigidbody I am not able to control smoothly the paddle.
What is the best way to handle this scenario?
Thank you.
答案1
得分: 1
以下是您要翻译的内容:
"What makes a air-puck feel good is the sliding effect it has. Ofcourse it doesn't continue forever, but still feels nice.
Here is what you can do:
- Create an Paddle & Puck
- Create two physics materials for both.
- Decrease the friction on the material that both feels slidey, the puck a little more than the paddle.
- For both:
- Freeze the x & z rotation
- Freeze the y position
Now the part that makes the paddle use physics & RigidBody correctly. Create a new script for moving the paddle:
public class PaddleMovement : MonoBehaviour
{
private RigidBody rb;
public float speed = 5;
public float minDist = 0;
public maxDist = 5;
public LayerMask layers;
void Start()
{
rb = GetComponent<RigidBody>();
}
void Update()
{
// Paddle will only move if we hold down the mouse button
paddleGrabed = Input.GetInput(KeyCode.Mouse0);
}
void FixedUpdate()
{
if (paddleGrabed)
{
HandleMovement();
}
}
void HandleMovement()
{
Ray ray = Camera.main.ScreenToWorldPoint(Input.MousePosition);
RaycstHit hit;
if (Physics.Raycast(ray, out hit, 100f, layers))
{
// Calculate the slow effect as paddle comes close to the point;
float dist = Vector3.Distance
(
new Vector3(transform.position.x, 0 transform.position.z),
new Vector3(hit.point.x, 0, hit.point.z)
);
dist = Mathf.Clamp(dist, minDist, maxDist);
var slowEffect = dist / maxDist;
// Now move move the rigid body appropriately
var dir = new Vector3(hit.point.x, 0, hit.point.z) -new Vector3(transform.position.x, 0 transform.position.z);
dir.Normalize();
rb.MovePosition(transform.position + dir * slowEffect * speed * Time.deltaTime);
}
}
}
That should move the Paddle to a certain position with a bit of a lag and sliding effect."
英文:
What makes a air-puck feel good is the sliding effect it has. Ofcourse it doesn't continue forever, but still feels nice.
Here is what you can do:
- Create an Paddle & Puck
- Create two physics materials for both.
- Decrease the friction on the material that both feels slidey, the puck a little more than the paddle.
- For both:
- Freeze the x & z rotation
- Freeze the y position
Now the part that makes the paddle use physics & RigidBody correctly. Create a new script for moving the paddle:
public class PaddleMovement : MonoBehaviour
{
private RigidBody rb;
public float speed = 5;
public float minDist = 0;
public float maxDist = 5;
public LayerMask layers;
void Start()
{
rb = GetComponent<RigidBody>();
}
void Update()
{
// Paddle will only move if we hold down the mouse button
paddleGrabed = Input.GetInput(KeyCode.Mouse0);
}
void FixedUpdate()
{
if (paddleGrabed)
{
HandleMovement();
}
}
void HandleMovement()
{
Ray ray = Camera.main.ScreenToWorldPoint(Input.MousePosition);
RaycstHit hit;
if (Physics.Raycast(ray, out hit, 100f, layers))
{
// Calculate the slow effect as paddle comes close to the point;
float dist = Vector3.Distance
(
new Vector3(transform.position.x, 0 transform.position.z),
new Vector3(hit.point.x, 0, hit.point.z)
);
dist = Mathf.Clamp(dist, minDist, maxDist);
var slowEffect = dist / maxDist;
// Now move move the rigid body appropriately
var dir = new Vector3(hit.point.x, 0, hit.point.z) -new Vector3(transform.position.x, 0 transform.position.z);
dir.Normalize();
rb.MovePosition(transform.position + dir * slowEffect * speed * Time.deltaTime);
}
}
}
That should move the Paddle to a certain position with a bit of a lag and sliding effect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论