2
\$\begingroup\$

I am making a game that slingshots an object using a line renderer. I wanted to know how I can throw my object when I unclick my mouse without having a curve trajectory like throwing a basketball to a ring. I already coded some script that will make an object throw but with a curve.

Here's an example that I want to achieve?

RED - How I want my object to happen.

BLUE - How I don't want my object to happen.

enter image description here


CODE


public float power; public Vector2 minPower; public Vector2 maxPower; Vector2 force; void Update() { MouseControl(); } void MouseControl() { //When touched if (Input.GetMouseButtonDown(0)) { //CODE } //When touch is hold if (Input.GetMouseButton(0)) { //CODE } //When touch is release if (Input.GetMouseButtonUp(0)) { force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y)); myBody.velocity = force * power; } } 
\$\endgroup\$
6
  • 1
    \$\begingroup\$ The curve is happening because of gravity, so disable gravity and it will go straight \$\endgroup\$ Commented Jun 17, 2020 at 11:42
  • \$\begingroup\$ That won't let my object fall if I disable it. \$\endgroup\$ Commented Jun 17, 2020 at 11:43
  • 1
    \$\begingroup\$ Try disabling the gravity of the object before throwing it, then trow object and check object velocity if it reaches 0 or nearly 0 then enable the gravity. \$\endgroup\$ Commented Jun 17, 2020 at 11:47
  • 5
    \$\begingroup\$ When, exactly, should it fall? If you can check for that, let gravity affect the object from that instant on, not before. I'm guessing you want it have zero velocity at that point, so it is free fall. \$\endgroup\$ Commented Jun 17, 2020 at 12:06
  • \$\begingroup\$ I just did and yeah It works! Thanks \$\endgroup\$ Commented Jun 17, 2020 at 12:09

2 Answers 2

2
\$\begingroup\$

First disable the gravity of the objects rigidbody when it gets spawned. You can do that in the inspector of the prefab for your projectile, or you can do it with code.

For a 3D rigidbody you do that with:

GetComponent<Rigidbody>().useGravity = false; 

For a 2D rigidbody you do that with:

GetComponent<Rigidbody2D>().gravityScale = 0f; 

When the object collides with something, reactivate gravity so that object falls down freely.

for 3D:

void OnCollisionEnter(Collider other) { GetComponent<Rigidbody>().useGravity = true; } 

for 2D:

void OnCollisionEnter2D(Collider2D other) { GetComponent<Rigidbody2D>().gravityScale = 1f; } 
\$\endgroup\$
1
\$\begingroup\$

Before Trowing object just disable the gravity of the object and upon reaching to the destination or by reaching the velocity of the object to 0 or nearly 0 then reenable object gravity so that object falls down freely.

I hope this will achieve the required effect.

(Converting comments to an answer so that next person in need can find it easy).

Thanks to @rtpax, @Theraot for commenting solution.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.