-1

Is it possible to move a gameobject to click position in unity 2D while gameobject is a rigidbody 2d with gravity and movement looks like jump from gameobject's position to click position. Any help reference will be really helpful :)

Anil
  • 15
  • 1
  • 8
  • I tried 2-3 methods 1. to follow click but in this it doesn't look like it jumping. 2. calculate distance between click and player than add force. Some time it works some time not. 3. Move player to click position. I want to jump at click position – Anil Sep 01 '15 at 07:46
  • Well, this is probably going to require a few physics calculations. This might help: [Trajectory of a Projectile](https://en.wikipedia.org/wiki/Trajectory_of_a_projectile) – Steven Mills Sep 01 '15 at 11:49

1 Answers1

2

something a bit like this?

Transform projectile;
float speed = 5f;

void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        target.z = projectile.position.z;

        Vector3 offset = target - projectile.position;
        Vector3 direction = offset.normalized;
        float power = offset.magnitude;
        projectile.GetComponent<RigidBody2D>().AddForce(direction * power * speed);
    }
}
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37