Unity3d: Backspin of Sphere with Rigidbody

2.2k views Asked by At

I am by any means not a physics expert, hence I can't really tell how the effect I am looking for is called. I'll just call it "Backspin" for now :)

Picture this scenario:

There is a ping pong ball in front of you on a ping pong table. You put pressure with your finger on it (going from the top of the ball to the bottom with a lot of force). The ping pong ball will slip off your finger moving forward but with a backwards spin. So, eventhough it moves forward it will eventually roll back to you due to its backwards spin :D

I hope that makes sense. I don't really know how this effect is called so I can't really look up solutions.

I do have a simple plane with a sphere (rigidbody attached) on it. I want to simulate this exact effect by swiping over the screen. But most importantly, how can that effect be achieved? The effect should also be effected by exactly where pressure is put on the ball (left or right). I just want it to move forwards while spinning backwards and eventually rolling back due to its backforce (not to exactly the same spot but more realistic).

I've experimented with Addforce and Addtorque but no success.

Any help is highly appreciated.

Have a nice day!

EDIT: What I have so far:

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);
    }
}

Edit2:

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;
}
1

There are 1 answers

3
Garren Fitzenreiter On BEST ANSWER

Start a new project. Add a terrain and a sphere. Add a rigidbody to the sphere and change the Drag to 1. Set the ball on top of the terrain in the middle. Add this code to your sphere gameobject

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);
}

Viola. Please note that the code is placed in start method and not update since the force should not be added continuously