在Unity3d中,如何在与物理引擎交互时使用鼠标移动游戏物体?

huangapple go评论62阅读模式
英文:

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:

  1. Create an Paddle & Puck
  2. Create two physics materials for both.
  3. Decrease the friction on the material that both feels slidey, the puck a little more than the paddle.
  4. 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:

  1. Create an Paddle & Puck
  2. Create two physics materials for both.
  3. Decrease the friction on the material that both feels slidey, the puck a little more than the paddle.
  4. 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&lt;RigidBody&gt;();
	}
	
	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.

huangapple
  • 本文由 发表于 2023年2月7日 04:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定