- Home /
How can i initiate an object in parabolic motion without using gravity using Rigidbody2D?
Actually i want the object(attached Rigidbody2d ) to be initiated at the bottom of screen and then follow a parabolic path (generated differently using different angles) and thus hit a target which is located at center of screen? not using any gravity . i am using Rigidbody2D to this game object and would like to generate a parabolic path using physics (eg rigidbody2d.addforce or rigidbody2d.velocity) rather than Translate.
Please help me with this Regards
is the starting point always the same and do you want to create ANY parabolic path or do you only want paths that exactly end in the center position?
No starting point changes but target is always same . I want to create a curved path similar to the image. If its possible the the gameobject follow different paths every time its initiated but hit the target.
Answer by Captain_Pineapple · Apr 19, 2018 at 11:00 AM
Hey there,
i did some calculations and the following should be sufficient for your problem. I assume that you have a Starting point A and a Target Point B, Scalar starting velocity v0 and an angle alpha in degrees(!).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrajectoryScript: MonoBehaviour {
public float v0;
public float alpha;
public float a;
public bool start = false;
private Transform A;
public Transform B;
public void FixedUpdate()
{
if (start) {
GetComponent<Rigidbody> ().AddForce (Vector3.right * a * GetComponent<Rigidbody> ().mass);
}
}
public void Update()
{
if (Input.GetKeyDown (KeyCode.Return)) {
calculateTrajectoryValues ();
}
}
public void calculateTrajectoryValues()
{
A = transform;
alpha = Mathf.PI * alpha / 180.0f;
v0 = v0 * Mathf.Sign (Mathf.Cos (alpha));
if (Mathf.Cos (alpha) == 0) {
alpha *= 0.99f;
}
a = (2 * v0 * v0 * Mathf.Pow (Mathf.Cos (alpha), 2.0f) * (B.position.x - A.position.x - (B.position.y - A.position.y) * Mathf.Tan (alpha))) / (Mathf.Pow (B.position.y - A.position.y, 2.0f));
GetComponent<Rigidbody> ().velocity = new Vector3 (v0*Mathf.Sin(alpha),v0*Mathf.Cos(alpha));
start = true;
}
}
This Script has to be attached to your object that you want to fly/throw at the target. It has to have a rigidbody attached. The public Variable B in the script has to be assigned with your targets transform.
I hope this helps (tested it and worked for me). In case you need the calculations behind this solution let me know. I can try to explain what i did and why/how it works.
If you need randomly initialized values stick to the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrajectoryScript: $$anonymous$$onoBehaviour {
public float v0;
public float alpha;
public float a;
public bool start = false;
private Transform A;
public Transform B;
public bool setRandomValues = true;
public void Start()
{
if (setRandomValues) {
v0 = Random.Range (1.0f, 15.0f);
alpha = Random.Range (-80.0f, 80.0f);
}
}
public void FixedUpdate()
{
if (start) {
GetComponent<Rigidbody> ().AddForce (Vector3.right * a * GetComponent<Rigidbody> ().mass);
}
}
public void Update()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)) {
calculateTrajectoryValues ();
}
}
public void calculateTrajectoryValues()
{
A = transform;
alpha = $$anonymous$$athf.PI * alpha / 180.0f;
v0 = v0 * $$anonymous$$athf.Sign ($$anonymous$$athf.Cos (alpha));
if ($$anonymous$$athf.Cos (alpha) == 0) {
alpha *= 0.99f;
}
a = (2 * v0 * v0 * $$anonymous$$athf.Pow ($$anonymous$$athf.Cos (alpha), 2.0f) * (B.position.x - A.position.x - (B.position.y - A.position.y) * $$anonymous$$athf.Tan (alpha))) / ($$anonymous$$athf.Pow (B.position.y - A.position.y, 2.0f));
GetComponent<Rigidbody> ().velocity = new Vector3 (v0*$$anonymous$$athf.Sin(alpha),v0*$$anonymous$$athf.Cos(alpha));
start = true;
}
}
Your answer
Follow this Question
Related Questions
How can i initiate an object in parabolic motion without using gravity using Rigidbody2D? 0 Answers
Do rigid bodies with force add force to other objects? 0 Answers
Gameobject going past target when using rigidbody2d.addforce 1 Answer
Rigidbody2D.AddForce works only once? 1 Answer
Rigidbody2D.velocity is not working. 0 Answers