Wrong Result !! why?

hi all …
i tried to make an object move in a circle way … so i write the equation of the circle based on time to move the object like this:

function Update () {
   var xPosition = Mathf.Sin (Time.time*Speed)*AU;
   var zPosition = Mathf.Cos (Time.time*Speed)*BU;  transform.localPosition=Vector3(xPosition,0,zPosition);
}

the movement of the object is look good if the Speed is a little amount , but when i change the Speed variable it doesn’t give me a correct result as shown below,i used Speed = 300, and i added Trail Render to show the object path.
any help??

Not sure what AU and BU are, but try this instead(untested):

var theta : float = 0;
var degreesPerSecond : float = 1;
var radius : float = 1;

function MoveInCircle()
{
   transform.position.x = radius*Mathf.Cos(theta);
   transform.position.z = radius*Mathf.Sin(theta);
   theta += Mathf.Deg2Rad(degreesPerSecond)*Time.deltaTime;
}

function Update()
{
   MoveInCircle();
}

Use degreesPerSecond to control the speed. However, if you must do it the way you are trying to do it, assign the time to a variable before calculating position, like so:

function Update () { 
   var currentTime = Time.time;
   var xPosition = Mathf.Sin(currentTime*Speed)*AU; 
   var zPosition = Mathf.Cos(currentTime*Speed)*BU;  
   transform.localPosition=Vector3(xPosition,0,zPosition); 
}

What I suspect is happening in yours is that there is a time difference between when you calculate the x and when you calculate the z. The difference would be very small, so you wouldn’t notice when speed is small. However, with speed set much higher, you’re scaling the time difference up, making the flaw more noticeable.

It is not possible to invoke an expression of type ‘float’

that’s the error that Unity gives when using this function.
but what is the value for convert the theta from Deg to Rad ??

i put a variable carrying the Time.time and use it like u said but it gives the same result too…
also i used FixedUpdate() function,FixedDeltaTime also,DeltaTime,all the time functions and it gives the same result…

the AU and BU is the coefficient of the circle to change it from a regular circle to ellipse.

still don’t know where is the problem :frowning:

yeah. Sorry. I always make that mistake. change it to:

theta += Mathf.Deg2Rad*degreesPerSecond*Time.deltaTime;

Mathf.Deg2Rad is a value, not a function, but I always think of it as a function. It just equals PI/180.

Ahh…thought you were just trying to make it go in a circle.

Not sure if it would have any effect, but try using transform.position instead of transform.localPosition.

i used it but it give me the same result when i increase the Speed variable :cry:

the local position what i need here , and if the object has no parent the local position and the position r the same … u try to solve the problem in wrong path, sorry

ok. gimme a few mins to check the math on paper. The fact that speed increases the problem still makes me think there is an error in calulating either time or a rounding error. I’m not at home, so I have to do it the old fashioned way.

Do a check for me. Take out the AU and BU in yours and run it. See if it does a circle. Make sure you set the speed high.

i tried it … if i put the radius instead of AU and BU it will draw a circle… but when i increase the Speed it give me the same wrong result.

i tried every kind of time functions on Unity and that didn’t help, i use a counter variable also increases it +1 evrey frame and use it instead of the time and it gives me the same wrong result…

that lead me to be crazy :shock:

really don’t know where is the problem !!!

Ok. The math is sound, and now I’m wondering if it is the trail renderer itself that is causing the problem. Try this. There is a setting in the trail renderer that determines how far apart the verticies of the trail are drawn. Set it really low. You’ll see a performance hit, but I need to rule this out. It might be that, when you are moving the object very fast, one vertex on the trail is drawn, and by the time the next one is drawn, the object is already on the other side of the ellipse (resulting in lines crossing over from one side to the other).

Your code is fine but you are driving the sphere around more than 100 times per second. Unless your framerate is extremely high, the object will take a full circle more than once per frame and since the TrailRenderer only samples the path every frame, you basically get sampled random points on the circle.

If you want a trail for extremely fast moving things, you need to find another solution than a TrailRenderer, and make it sample the path many times per frame.

Rune

With extremely high values for speed, like the 300 in your example, the object will traverse a large part of the circle per frame. You draw lines between positions of subsequent frames. Since your object moves at speeds of more than 20 degrees per frame, these lines will obviously go right through the center of your circle. Your object still properly follows the circle, though.

In other words, everything is working fine. There is simply a misconception about what reasonable values for speed are. Note that a value of 2*PI for Time.deltaTime * speed is already a speed of a full circle per frame. Simply choosing lower values for speed should yield more reasonable results.

EDIT: yes, I’m slow ;).

i’ve tried to use a model created on Maya for the Orbit, i do not want it to look thick,and when i used it on Unity i had the Anti-Aliasing Problem like shown below :

that why i thought the trail render will look good.

anyone has another ideas how to solve the anti-aliasing problem or any other ideas for showing the orbit model??

P.S : i assign the Anti-aliasing in the Quality menu to 16x with no result.

Put a texture on your orbit object that is opaque in the center and transparent on both sides. Basically, you can do the exact same thing here that you could have done with the TrailRenderer.

Rune