Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Adding curve to a ball

Discussion in 'Scripting' started by beeflilly, Apr 19, 2020.

  1. beeflilly

    beeflilly

    Joined:
    Apr 1, 2020
    Posts:
    6
    I'm curious as to how I could go about adding the ability for a ball to curve in the air. Similar to that of a soccer ball.

    I currently have a ball in my scene that I can shoot with a gun and add force to it using AddForce, as well as rotation using AddTorque, based off the normal the Raycast hits. So now I'm trying to figure out how I can add curve based off the rotation of the ball while it's in the air. Is that possible?

    video of scene: https://imgur.com/qoALQVo

    Here is my code that interacts with all Rigidbodies, which is what i'm using to interact with the ball.
    Code (CSharp):
    1.  public void Shoot ()
    2.     {
    3.         transform.localPosition -= new Vector3(0, 0, recoil);      
    4.        
    5.         RaycastHit hit;
    6.         if (Physics.Raycast(fpsCam.position, fpsCam.forward, out hit, range))
    7.         {        
    8.             Enemy enemyTarget = hit.transform.GetComponent<Enemy>();
    9.             if (enemyTarget != null)
    10.             {
    11.                 enemyTarget.TakeDamage(damage);
    12.             }
    13.            
    14.             if (hit.rigidbody != null) //affecting the ball in the scene
    15.             {
    16.                 hit.rigidbody.AddForce(-hit.normal * hitForce);
    17.                 hit.rigidbody.AddTorque(hit.normal * torqueForce, ForceMode.Force);
    18.             }
    19.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    20.             Destroy(impactGO, 1f);
    21.         }
    22.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I would start with reading a little about the Magnus Effect: https://en.wikipedia.org/wiki/Magnus_effect#In_sport. Then basically try to figure out what the extra force on the ball would be, and use a script on the ball itself to try to calculate that force in FixedUpdate() based on the current properties of the ball's Rigidbody: Mass, Velocity, Rotation etc.. You'll have to figure out what the math is based on how the magnus effect works, but remember you don't need to be totally accurate, you probably are looking for a fun gameplay mechanic, so tune your script that in mind. Basically the code would look something like this:
    Code (CSharp):
    1. void FixedUpdate() {
    2.   var rb = GetComponent<RigidBody>();
    3.   var forceToApply = /* Some magic formula involving rb.angularVelocity, rb.velocity, some constants from wikipedia */
    4.   rb.AddForce(forceToApply);
    5. }
     
    beeflilly likes this.
  3. beeflilly

    beeflilly

    Joined:
    Apr 1, 2020
    Posts:
    6
    Thanks for your reply! I think that gave me a lot of information on how to solve this. I'm not a physics guy so we will see how this goes! I'm enjoying the brain stimulation that comes along with learning game programming. It's really fun!