Search Unity

Parabola arc style flight path without knowing the destination x/y?

Discussion in 'Scripting' started by LostRelicGames, Oct 31, 2014.

  1. LostRelicGames

    LostRelicGames

    Joined:
    Aug 11, 2013
    Posts:
    34
    Hi Guys,

    There are several threads discussing parabola formula and creating arced throwing, all I found seem to require a destination target x/y ie. basketball and a hoop

    Question: What is the most efficient way to script the throwing of a rotating object along an invisible arced flight path, with no specific target object (in 2D space), so that the thrown object continues to fall indefinitely until OnCollisionEnter2D?

    I have tried using addForce and a rigid body attached to the object, however
    from tests, I found rotation is effecting my velocity/flight path.

    TY.




     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The real easy thing would be to just stop rotation of the object, but I'm assuming that wouldn't look very great.

    A bit more complicated, but still physics-based would be to give the axe or whatever you're throwing there a parent with a rigidbody, and move that with force, and spin the underlying object manually.


    What I think you should do, though, is to move the thing yourself through a script. That'll avoid unwanted interference from the physics engine, and allow your players to predict the movement of the thrown object better.. You could go real old-school, and just write a function that takes starting position and time as arguments, and returns what position the thing should have now. Then you move it with RigidBody.MovePosition to ensure that collisions are registered.

    If you do that, I'd suggest using an animation curve to give height over time - simply add a public animationcurve to your object, edit it in the editor, and read it off in the script.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Do you care if the angular velocity of the object (that looks like an axe?) accurately impacts the translational velocity of the object?

    Also do you want to actually calculate the full path for display purposes? Or just apply it to an existing object?

    If you're just trying to apply it to something. You have 2 primary options:

    1) Rigidbody - just give the initial force, unity will take care of the rest

    2) simulated - have an initial velocity, apply gravity to that velocity every frame (using deltaTime), apply that velocity every frame (multiply by deltaTime)


    If you want to calculate the path Rigidbody would take if you applied some initial force. Well you need trajectory of a projectile, a fundamental concept in Newtonian Physics.

    http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

    You're going to want to consider the 'angular drag' in the calculation of the trajectory. You know the value as it's configured in the settings of the Rigidbody.
     
  4. LostRelicGames

    LostRelicGames

    Joined:
    Aug 11, 2013
    Posts:
    34
    Thanks for your responses guys, I wanted to have a little play based on the info provided before I posted some findings.

    Below is the code I ended up going with. although it is not super realistic in the context of real world physics, it suits the game.

    One qwerk is that the Axe throw distance will dramatically change if the player is stuck up against a wall, stationary or running.
    Checking the players velocity.x always returns the same number regardless of whether they are visually moving or not. Is there another way to gauge the players current speed and append it to the Axe starting velocity?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ThrowSimulation : MonoBehaviour
    5. {
    6.     public float Vx;
    7.     public float Vy;
    8.     public float gravity = 10f;
    9.    
    10.     private Transform player;
    11.  
    12.     void Awake()
    13.     {
    14.         player = GameObject.Find ("Player").transform;
    15.     }
    16.  
    17.     void Start()
    18.     {    
    19.         doThrow();
    20.     }
    21.  
    22.     void doThrow()
    23.     {
    24.         transform.position = player.position;
    25.         Vy = 4; // little arc
    26.         Vx = player.rigidbody2D.velocity.x*2;
    27.  
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         rigidbody2D.velocity = new Vector2(Vx, Vy);
    33.         Vx -= (gravity * Time.deltaTime)/10;
    34.         Vy -= (gravity * Time.deltaTime);
    35.  
    36.         transform.Rotate (Vector3.back * 15);
    37.     }
    38. }