How to throw a ball up in the air in 2D

Hi

What’s the best way to simulate an object being thrown upwards and on any different upward angle that I want and then having the object fall back down towards the bottom of the screen restricting this all to 2D. What would be the most suitable function to use? and any known gotchas?

Cheers

The code below will work fine, but you need to put your objects at origin( x=0, y=0, z=0). It also randomize every possible value.
Or you can also use Unity’s physics components(Gravity, force,…)

private var Accel : float = 0;
private var RandomPosition : float;
private var PullDown = false;
private var XRot=0;
private var YRot=0;

function Start()
{
    transform.position.x=Random.Range(-50,50);
    RandomPosition = Random.Range(-40,40);
    XRot=Random.Range(-150,150);
    YRot=Random.Range(-150,150);
}
function Update () 
{
    transform.position.x+=Rnd*Time.deltaTime;
    transform.position.y+=Accel*Time.deltaTime;
    transform.Rotate(XRot* Time.deltaTime,YRot* Time.deltaTime,0);
	
  if(PullDown==false)
  {
    Accel += 250* Time.deltaTime;
  }
  if(transform.position.y > 100 )
  {
    PullDown=true;
    Accel -= 280* Time.deltaTime;
  }
}

Thanks akurdas for sharing :slight_smile:

I’m having some fun with this code tweaking the values and observing the results. The visual results of this code is getting the desired result and I still have some tweaking and play testing to go.

Thanks.