1

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;
}
user2669261
  • 396
  • 1
  • 5
  • 18
  • 1
    can you not add a negative rotate amount to the gameobject and use add force to move it forward. You could also add a physics material that will affect the way it reacts with a surface. This may help you http://answers.unity3d.com/questions/377944/how-to-make-a-ball-rotate-as-per-its-moving-direct.html] – Alan-Dean Simonds Dec 14 '16 at 01:57

1 Answers1

2

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

Garren Fitzenreiter
  • 781
  • 1
  • 5
  • 22
  • Hey, thanks! I'm getting closer. I updated my question with the code I have so far. I changed your code slightly. The sphere now moves forward and eventually comes back. But: It seems to behave like it's on ice, like spinning really fast backwards but just moving slowly, which is great but I'd like it to have a little more friction so it comes back faster. Any idea how to achive that? – user2669261 Dec 14 '16 at 11:42
  • right click on an empty space in your project window and create a new physics material. change the friction values higher. the higher the number the more grip it will have and wont travel as far. add it to your sphere and terrain collider – Garren Fitzenreiter Dec 14 '16 at 14:44
  • Hey. Thanks. I wasn't very satisfied with the physics material, so I came up with another approach (edit in main post): I just set another Addforce once the spere reaches magnitude= 0. Looks just like I wanted it. Thanks for your help! – user2669261 Dec 14 '16 at 15:42