- Home /
Calculating and drawing trajectory. I need mathematician
I have a code of moving GO to points that player clicked. How can I draw his trajectory after clicking the points but before moving. Help me please. I need mathematician to make some formula to calculate the trajectory
var minSpeed : float = 10;
var maxSpeed : float = 50;
var moveSpeed : float;
var accSpeed : float = 20;
var breakSpeed : float = 40;
var rotateSpeed : float = 2;
var breakDist : float = 30;
private var targPos : Vector3;
function Update () {
targPos = tarPoints[0]; //massive with clicked points.
var targDir = targPos - transform.position;
var targDist = Vector3.Distance(transform.position, targPos);
//If distance more then 3
if (targDist > 3) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targDir), rotateSpeed * Time.deltaTime); //rotating
//Moving forward
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
//Accelerating. if not braking
if(moveSpeed < maxSpeed && !breaking){
moveSpeed += accSpeed * Time.deltaTime;
}
//Braking
if(targDist < breakDist && moveSpeed > minSpeed){
//at breakDist to last point or if go didn't rotate in time
if(tarPoints.length == 1 || Mathf.Abs(Quaternion.Dot (transform.rotation, Quaternion.LookRotation(targDir))) < 0.95){
breaking = true;
moveSpeed -= breakSpeed * Time.deltaTime;
}else{
breaking = false;
}
}else{
breaking = false;
}
}
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
You should be able to make something with my answer here ($$anonymous$$ $$anonymous$$ay): http://answers.unity3d.com/questions/288067/drawing-lines-between-two-touch-positions.html
Just find a way of making the player inputPosA
, and the clickpoint inputPosB
inputPosA
would be the player transform, inputPosB
would be a ScreenPointToRay
Also, put var myLine : Transform;
at the top, then change the line in the function to myLine = Instantiate( myCube....
, then before drawing the next line, destroy myLine
so the old one disappears first =]
Trajectory will not like line it'll like arc. And I need formula to calculate the trajectory.
so the object follows a path of clicks, like waypoints? Or is it 'Angry Birds' style?
Check here : http://answers.unity3d.com/questions/168505/Trajectory-aim-how-do-i-do-it.html
Or just search the forum for Trajectory : http://search.unity3d.com/uss1/?q=trajectory&type_of_search=answers≈p=All
Answer by Bunny83 · Aug 02, 2012 at 12:17 PM
Like Mike already said, it's impossible to predict the movement of your objet because of the way you move it. Your smoothing function for the rotation is not frame-independent.
It would be possible if you do all your movement in FixedUpdate since you don't have fluctuations in the framerate, but's it's not an easy task the way you move your objects.
In such a case the easiest but not the most efficient way is to just iterate into the future with a copy of your true values. Since you use object functions like Translate which depends on the current object state (rotation), it's not possible that way you have it at the moment.
edit
Here's an example how it could be done.
And here's how it looks like ;)
But usually if you really need something like that, do it with splines since it's more accurate.
@@@there is no exact formula for this sorta trajectory but yeah you can find out the equation if the values of variables are known at least three or four points , by using curve fitting method@@@
Can this method help me?
Just in case you missed my edit, i've added a sample script to my answer ;)
Answer by whydoidoit · Aug 02, 2012 at 09:29 AM
What you are asking for doesn't have a purely mathematical solution because of the issues of the direction of the vehicle (at least at the start). You could use a spline path follower like mine and just iterate through the path and draw the points using Vectosity or LineRenderer (see the gizmos in my link which do this trick at edit time). That won't fix the fact that your follower is pointing the wrong way though.
So if you want to simulate that then you are going to have to run the logic for the follower for real (but inside a loop, not actually moving anything in Update), sample 100 or so positions and then draw a line between each of these.
when I using my code in loop (but without Time.deltaTime, using some float ins$$anonymous$$d of), unity is freezing (crashing, hanging (i don't know how to say it in English)). Can you edit my code in first post if I'm doing something wrong?
Ok it sounds like you've just not got a correct loop ter$$anonymous$$ation condition - you still want to simulate time - just make one up for the increment.
I used while(targDist> 3) ins$$anonymous$$d of if(targDist> 3). What must I do?
Your answer
![](https://koobas.hobune.stream/wayback/20220613075020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can we have the player creating the trajectory of a car before a collision? 0 Answers
DrawMesh always drawn before gameobjects 1 Answer
create line on a texture 2 Answers
Debug.draw rect 2 Answers