# Creating a arc path connecting two GameObjects in Unity

I am working on a game on a spherical surface which first of all is a big challenge in itself.

I want to create a path on an arc which connects two points ( seen in the image ) and show an arc path that connected the two points I was exploring some functionality in iTween plugin that created projectile paths from source to target. Would that be ideal to my problem? Or is there any functionality that I could use within Unity itself that would make this possible? Or if anyone knows of a any Unity plugin on the Asset store that would make this work, I would be willing to use it try it too.

After some work I thought about shooting a projectile from source to target. I found a script for the same on Unity forums. The below code works for that

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ProjectileTestScript : MonoBehaviour {

public Transform target;
Vector3 finalVel = Vector3.zero;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.GetKeyDown(KeyCode.E))
{
Vector3 throwSpeed = calculateBestThrowSpeed(gameObject.transform.position,target.transform.position,2.0f);
}
}

private Vector3 calculateBestThrowSpeed(Vector3 origin, Vector3 target, float timeToTarget)
{
// calculate vectors
Vector3 toTarget = target - origin;
Vector3 toTargetXZ = toTarget;
toTargetXZ.y = 0;

// calculate xz and y
float y = toTarget.y;
float xz = toTargetXZ.magnitude;

// calculate starting speeds for xz and y. Physics forumulase deltaX = v0 * t + 1/2 * a * t * t
// where a is "-gravity" but only on the y plane, and a is 0 in xz plane.
// so xz = v0xz * t => v0xz = xz / t
// and y = v0y * t - 1/2 * gravity * t * t => v0y * t = y + 1/2 * gravity * t * t => v0y = y / t + 1/2 * gravity * t
float t = timeToTarget;
float v0y = y / t + 0.5f * Physics.gravity.magnitude * t;
float v0xz = xz / t;

// create result vector for calculated starting speeds
Vector3 result = toTargetXZ.normalized; // get direction of xz but with magnitude 1
result *= v0xz; // set magnitude of xz to v0xz (starting speed in xz plane)
result.y = v0y; // set y to v0y (starting speed of y plane)

return result;
}
}


Could I now create a path trajectory with all the calculated points at a certain time from source to destination, store those points and then create a path? Would that work?

I just recently got Vectrosity but I am not sure how that would work here. If someone could give me a direction as to what functionality from Vectrosity would help me solve this problem?

Okay after looking into several topics on the internet, I found a cheap solution which I am not sure would work

so basically the idea is to instantiate new gameObjects with lineRenderers attached on them and drawing lines based on the current position and future positions of my projectile in motion. Here is the code that does that

void Update () {

if (Input.GetKeyDown(KeyCode.E))
{
throwVal = true;
Vector3 throwSpeed = calculateBestThrowSpeed(gameObject.transform.position, target.transform.position, 4.0f);

}
//at every 0.5f seconds draw the line from lineRenderer using objects current position and target position

if (Vector3.Distance(transform.position, target.transform.position) < 0.2f)
{
reachedTarget = true;
}

if (currTime + delay < Time.time && !reachedTarget)
{
currTime = Time.time;
futureCurrentPosition = transform.position;
DrawLine(initialPosition, futureCurrentPosition);
initialPosition = futureCurrentPosition;
}
}

private void DrawLine(Vector3 pointA, Vector3 pointB)
{
GameObject lineRendGameObj = Instantiate(lineRendPrefab, transform.position, Quaternion.identity) as GameObject;
LineRenderer line = lineRendGameObj.AddComponent("LineRenderer") as LineRenderer;
line.material = lineMaterial;
line.SetPosition(0, pointA);
line.SetWidth(0.45f, 0.45f);
float dist = Vector3.Distance(pointA, pointB);
Vector3 final = dist*Vector3.Normalize(pointB-pointA) + pointA;
line.SetPosition(1, final);
}


I am not sure if this is ideal but I would still like to know if Vectrosity would do a better job with this. If someone has a better solution please let me know!

• Vectrosity (in asset store) might solve this for you. It's a solid library either way, which I've used for somewhat similar functionality. Though, looking at the doc .pdf, I'm not seeing "arc" functionality, so I suspect you'll have to calculate the intermediate points using Vector3.Slerp. – Chris Mills-Price Oct 15 '14 at 18:37
• On further thought, you may be fine with Unity's LineRenderer and Vector3.Slerp. I do like Vectrosity (and it offers good fuctionality), but if you're going to have to do arc logic yourself, you may prefer not to pay. – Chris Mills-Price Oct 15 '14 at 18:42
• I have no idea how efficient this would be, but you could simply seperate the straight connection between your two points in several lines with equal length and push their start and end outwards until their distance to the center is equal the radius. Could be horrible performancewise though. The more you seperate the line, the finer your final arc will be. I suspect though this does not qualify for a good answer. – J_F_B_M Oct 15 '14 at 19:57
• I am able to calculate the arc basically a projectile motion from source to target but how I want to visually show the arc path. I will check Vectrosity and see how it would work. Thanks! – ckzilla Oct 16 '14 at 1:05
• I got Vectrosity, could you please explain me what functionality should I be using to draw a line along the arc or rather a sample code that would explain it? – ckzilla Oct 16 '14 at 4:29