I have a 2.5D background and I have to throw a ball. Ball have to go towards screen.
As It’s 2.5D so what I have did so far:
(Note: There’s a specific distance ball can cover)
Technique no 1: I applied force upward and meanwhile changing scale of ball to look like It’s going towards screen. I faced No jerks, ball went up and came back smoothly but what I did face is ball move too slow. I have to move it fast but when I increase Force It goes beyond a specific distance.
public void Throw (float speed)
{
rigidbody2D.isKinematic = false;
rigidbody2D.AddForce(Vector2.up * speed, ForceMode2D.Impulse);
rigidbody2D.AddTorque(15);
t = 0;
InvokeRepeating("scaleDown", 0, 1f / 60f);
prevJumpValue = transform.localPosition.y;
InvokeRepeating("FindMidOfThrow", 0.1f, 1f / 60f);
}
// Adjusting Z value by adjusting scale of throwing item
private float t = 0;
void scaleDown()
{
t += Time.deltaTime * 0.4f;
float scale = Mathf.Lerp(1f, Constants.MIN_THROWING_ITEM_SCALE, t);
transform.localScale = new Vector3(scale, scale, scale);
if (scale == Constants.MIN_THROWING_ITEM_SCALE)
CancelInvoke("scaleDown");
}
private float prevJumpValue;
void FindMidOfThrow()
{
// If Y is still increasing then it's getting high, record the value
if (transform.localPosition.y > prevJumpValue)
prevJumpValue = transform.localPosition.y;
// If It's mid of throw
else
{
// Stop finding it already ;)
CancelInvoke("FindMidOfThrow");
// Turn on its collider so that it can land on platform
GetComponent<CircleCollider2D>().isTrigger = false;
}
}
Technique No 2: I manually handled transform of ball but at that specific height when ball have to come back to platform, I face a clear Jurk
Vector2 from = new Vector2(fromXVal, -2.2f);
Vector2 to = new Vector2(xVal, 1.7f);
float cosVal = (Mathf.Cos(currentTime / duration) + 1) / 2;
currentTime += Time.deltaTime;
transform.l![41434-untitled1.png|934x611](upload://hyimKN5eceaW4YldYrzdT8a6mbe.png)ocalPosition = Vector3.Lerp(to, from, cosVal);
Technique No 3: I manually handled velocity and I face the same Jurk when ball has to come back after a specific height
rigidbody2D.velocity = new Vector2(xVal, force);
Can Anybody please help how can I achieve this. Thanks in advance