- Home /
Projectile movement in 3D
I am trying to simulate a projectile artillery shot, I am using the projectile formulas to simulate this movement, but the problem is, I am not finding a way to transform this into 3D coordinates, I can get only the x and y.
What I want to do is transform the (x,y) coordinates I am calculating alongside the initial forward Vector of the gameobject associated with the following script:
#pragma strict
public class ArtilleryShot extends MonoBehaviour{
var startTime:float ;
var target:Transform;
private var destination:Vector3;
private var distanceToDestination:float;
private var velocity:float = 20.0;
private var vx:float;
private var vy:float;
private var g:float = 10.0;
private var x0:float;
private var y0:float;
var launchExplosion:GameObject;
var explosion:GameObject;
var damage:float;
var launchPoint:GameObject;
function Start(){
var explosion:GameObject = GameObject.Instantiate(launchExplosion,transform.position,transform.rotation);
explosion.transform.localScale = Vector3(4,4,4);
startTime = Time.time;
destination = target.position;
distanceToDestination = Vector3.Distance(transform.position, destination);
//velocity = Mathf.Sqrt((distanceToDestination*g)/(Mathf.Sin(Mathf.Deg2Rad*120)));
velocity = Mathf.Sqrt((distanceToDestination*distanceToDestination*g)/(2*Mathf.Cos(Mathf.Deg2Rad*60)*Mathf.Cos(Mathf.Deg2Rad*60)*(transform.position.y+(distanceToDestination*Mathf.Tan(Mathf.Deg2Rad*60)))));
vx = velocity * Mathf.Cos(Mathf.Deg2Rad*60);
vy = velocity * Mathf.Sin(Mathf.Deg2Rad*60);
x0 = transform.position.x;
y0 = transform.position.y;
}
function Update(){
var t:float = Time.time-startTime;
if(t==0)
return;
var newPos = transform.position;
newPos.x = x0+(vx*t);
var downY:float = (-.5*g*t*t);
var y:float = vy*t+downY;
newPos.y = y0+y;
var distance = Vector3.Distance(transform.position,newPos);
var angle = Vector3.Angle(launchPoint.transform.forward,newPos);
//newPos should be projected alongside the forward axis before setting the new position
transform.position = newPos;
}
function SetTarget(targetParam:Transform){
target = targetParam.transform;
}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="EnemyGroundUnit" || other.gameObject.tag=="Ground"){
var explosionGameObject = GameObject.Instantiate(explosion,transform.position,transform.rotation);
explosionGameObject.transform.localScale = Vector3(4,4,4);
other.gameObject.SendMessage("TakeDamage",damage);
Destroy(gameObject);
}
}
function SetPointOfOrigin(launchPointParam:GameObject){
launchPoint = launchPointParam;
}
}
Answer by NorthernVisionStudio · Jul 05, 2014 at 05:29 AM
You are very close! So, your Y component is the up/down and your x is the distance from start. You have the launch vector, which includes height. So, here's my opinion:
start with the launch vector: Vector3 launchVec = launchPoint.transform.forward
get rid of the y value: launchVec.y = 0
Now you have a flat but non-normalized vector. So, then normalize it. That leaves you with a top-view flat direction vector.
Then take that normalized vector and multiply it by your distance (x) value of your formula. Remember, this is a relative vector, so you will be adding it to the position
Finally, add the y value to this result... resultVec.y += y
And, add to existing position
I will leave it up to you to interpret and understand rather than me writing code.
In summary, the strategy is to use a flattened launch vector for the X and just add the Y component for height.
Your answer
Follow this Question
Related Questions
Shooting projectile : GameObject or Particles ? 2 Answers
Calculate Projectille impact point 2 Answers
Shooting projectile : GameObjects or Particles? 1 Answer
Inspector Problems or possible miss script. 0 Answers
Throwing a Spear 1 Answer