I am new bie to unity. I have an object eg cube. It should go for few distance like sine Wave.
I have found many code. They are using Sin Maths functions. I dont want to use sin mathf function.
Is there any code. the object move like sin wave without using sin mathf function.
The only way I know to do this movement is using sin math functions. There’s no reason to not use it. But I think you can try DOTween. Maybe it can help in your case.
Thanks Lefty Righty & diasrodrigo for your code. Actually i will try ur methods & update. The main reason i dont want to use for sin function.
The project is getting lag while i use that code. the fps comes very low.
So i think Sin function is the cause for lagging? So only i am try to avoid
You need to post the code for it. @bobisgod234 has it right, shouldn’t cause lag unless your running a whole lot of them at the same time. If you are running a whole lot of them any solution will need to be as optimized as possible to reduce overhead.
Thanks bobis & chris for your Post. Actually i am running a whole solution. So only i am trying to avoid sin. Is there any code for ball should go up and down and continuouly.
Like a sin wave.
i think we can use transform.up + Vector3 position. can any one can help me…It should go continuously like sin wave
It’s actually very easy to do and there are several ways to do it without using Mathf.sin.
One way is to simply apply a force in the vertical and horizontal directions and let gravity pull you back down. Another way is to move the transform itself without gravity. Note that if you want this object to interact with physics correctly use Rigidbody.MovePosition instead. Also note that direction and new Vector3.x need to be the same absolute value to have a perfect sine wave.
Public class sinWave()
{
float direction;
int maxLimit, lowerLimit;
Void start()
{
maxLimit = transform.position.y + 10;// you set this to your desired height
lowerLimit = transform.position.y;//this can be whatever you want as well
direction = 0.05f;// you can put this as any number you want, the larger the number the more it will look like teleporting, the smaller the number the more smooth but the less distance it will travel per frame/time
}
void FixedUpdate()
{
transform.position = transform.position + new Vector3(0.05, direction, 0);
if(transform.position.y >= maxLimit) direction = -0.05f;
if(transform.position.y <= lowerLimit) direction = 0.05f;
}
}
Thanks lefty Rights & StickyHoneybuns for your code. @sticky Honey buns - Thanks for your code. Your code is really what i have expect. It goes like sin wave.Thanks for your code.
But when ball go upwards and coming down. There is no smoothness like a curve.
How can i bring smoothness. when it goes upwards ok after it comes down i have to bring smoothness like a wave.
Is it possible through code. or sin function i have to implement.
@Alexander21 , I’ll just come out and say it: you’re being silly. Any way you can make an object move in a sine wave, without using Sin, is going to be more expensive than just using Sin. Sin is extremely fast. Whatever is the cause of your performance problem, that’s not it. By trying to avoid Sin, you are completely barking up the wrong tree.
Of course, just for fun, we could brainstorm ways to do it anyway:
You could model a ball and spring system, where there is a central position for your object, which has a velocity and mass, and a force that is proportional to its distance from this central position. You would then implement some integrator (the forward Euler method is simple and probably good enough) to update its position over time, and if your time steps are small enough, this will be a good approximation of a sine wave. Of course actually using Sin would be faster and more accurate.
You could use the Pythagorean theorem to calculate the correct position from the hypotenuse and leg of an imaginary triangle (with an angle that depends on the time). This one is more work than using Sin, but the results should be just as accurate.
You could have a (possibly invisible) GameObject that you rotate over time, and then use transform.TransformPoint to find the location of a point on an imaginary wheel rotating around that position. Then you’d set your object’s Y position to match, ignoring the X or Z. Of course under the hood, Unity is doing many Sin calls to accomplish this for you.
So sure, if you insist on using a method that is harder and less efficient, there are many ways to do it… these are just a few.
Maybe use a single animation curve for all objects.
You can evaluate its height at x for each object. That sounds pretty cheap to me.
Apparently someone did a comparison as well.
You might get some gains. You won’t get major gains, though.
You’re new to unity, so you should confirm what is causing the lag.
Do you have a lot of objects?
Objects are more expensive than sin calculations, so objects would be causing lag long before sin. Do you have any errors in the debug log? That can cause a lot of lag if there are many of them.