【问题标题】:Unity3d: Backspin of Sphere with RigidbodyUnity3d:带刚体的球体后旋
【发布时间】:2016-12-14 01:20:17
【问题描述】:

我无论如何都不是物理学专家,因此我无法真正说出我正在寻找的效果是如何被调用的。我暂时称它为“Backspin”:)

想象一下这个场景:

在你面前的乒乓球台上有一个乒乓球。你用手指施加压力(用很大的力从球的顶部到底部)。乒乓球会从您的手指上滑落,向前移动但向后旋转。所以,即使它向前移动,由于它的向后旋转,它最终会回滚到你身边:D

我希望这是有道理的。我真的不知道这个效果是如何调用的,所以我无法真正查找解决方案。

我确实有一个简单的平面,上面有一个球体(附加刚体)。我想通过在屏幕上滑动来模拟这种精确的效果。但最重要的是,如何才能达到这种效果?效果也应该受到压力施加在球上的确切位置(左或右)的影响。我只是希望它在向后旋转的同时向前移动,并由于它的反作用力最终回滚(不是完全相同的位置,而是更逼真)。

我已经尝试过 Addforce 和 Addtorque,但没有成功。

非常感谢任何帮助。

祝你有美好的一天!

编辑:到目前为止我所拥有的:

public float spin = 1000f;
public float force = 20f;
public float angularVelocity = 100f;
private Rigidbody rb;
private Vector3 oldPos;
private Quaternion oldRot;
public int pressed = 0;

void Start () {
    oldPos = transform.position;
    oldRot = transform.rotation;
    rb = GetComponent<Rigidbody> ();
    rb.maxAngularVelocity = angularVelocity;
}

void FixedUpdate ()
{
    Debug.Log(rb.velocity.magnitude);

    if (Input.GetKey(KeyCode.Space))
    {
        Debug.Log("pressed");
        pressed = 1;
        rb.AddForce(new Vector3(0,0,force),ForceMode.VelocityChange);
        rb.AddTorque (new Vector3 (-spin, 0, 0), ForceMode.Force);
    }
}

编辑2:

public float spin = 1000f;
public float force = 20f;
public float angularVelocity = 100f;
private Rigidbody rb;
private Vector3 oldPos;
private Quaternion oldRot;
public int pressed = 0;
public float forceTime = 1f;
public float startBack = 0.25f;

void Start () {
    oldPos = transform.position;
    oldRot = transform.rotation;
    rb = GetComponent<Rigidbody> ();
    rb.maxAngularVelocity = angularVelocity;
}

void FixedUpdate ()
{
    Debug.Log(rb.velocity.magnitude);
    if (pressed == 2)
    {
        rb.AddForce(new Vector3(0,0,-force),ForceMode.Acceleration);

    }
    else if (pressed == 1 && rb.velocity.magnitude <= startBack && rb.velocity.magnitude >= 0f)
    {
        pressed = 2;
        StartCoroutine("StopForce");
    }
    else if (Input.GetKeyDown(KeyCode.Space) && pressed == 0)
    {
        Debug.Log("pressed");
        rb.AddForce(new Vector3(0,0,force),ForceMode.VelocityChange);
        rb.AddTorque (new Vector3 (-spin, 0, 0), ForceMode.Force);
        pressed = 1;
    }
}
IEnumerator StopForce () {
    yield return new WaitForSeconds(forceTime);
    pressed = 3;
}

【问题讨论】:

  • 你能不能不给游戏对象添加一个负旋转量并使用加力向前移动它。您还可以添加会影响其与表面反应方式的物理材料。这可能会帮助你answers.unity3d.com/questions/377944/…

标签: unity3d physics unity5 rigid-bodies


【解决方案1】:

开始一个新项目。添加地形和球体。将刚体添加到球体并将 Drag 更改为 1。将球设置在中间地形的顶部。将此代码添加到您的球体游戏对象中

Rigidbody rb;

void Start () {
    rb = GetComponent<Rigidbody> ();
    rb.maxAngularVelocity = 100;
    rb.AddForce(new Vector3(20,0,0),ForceMode.VelocityChange);
    rb.AddTorque (new Vector3 (0, 0, 1000), ForceMode.Force);
}

维奥拉。请注意,代码放在start方法中,不更新,因为不应该连续添加力

【讨论】:

  • 嘿,谢谢!我越来越近了。我用到目前为止的代码更新了我的问题。我稍微更改了您的代码。球体现在向前移动并最终返回。但是:它的表现就像在冰上一样,就像非常快速地向后旋转但只是缓慢地移动,这很好,但我希望它有更多的摩擦力,所以它会更快地回来。知道如何实现吗?
  • 右键单击项目窗口中的空白区域并创建新的物理材料。将摩擦值更改得更高。数字越高,它的抓地力就越大,而且不会走得那么远。将其添加到您的球体和地形对撞机
  • 嘿。谢谢。我对物理材料不是很满意,所以我想出了另一种方法(在主帖中编辑):一旦 spere 达到幅度 = 0,我就设置另一个 Addforce。看起来就像我想要的那样。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 2019-05-03
  • 2013-11-04
  • 2017-05-13
  • 2023-03-22
相关资源
最近更新 更多