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);
rigidbody.AddForce(throwSpeed, ForceMode.VelocityChange);
}
}
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);
rigidbody.AddForce(throwSpeed, ForceMode.VelocityChange);
}
//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!