March 01, 2016

How to move object following sine curve in Unity?

Today I solved little task of moving objects following given direction. In my case it was sine curve.



Scene setup.

There are minimal set of objects on scene: camera, light, cube and sphere. Camera rotated by 90 degrees around X axis and lifted up by Y axis, so we're watching scene vertically. Cube has Rigidbody attached; property Use Gravity is disabled on it. Sphere has just a regular SphereCollider.





Mathematical background.

We want to move
Cube to Sphere adding sinusoidal vibration perpendicularly to the moving direction. At the beginning we need to calculate vector of moving direction and orthogonal vector for it. We will multiple orthogonal vector by result of sinus function (time is an argument).

Scripting.

Attach script Mover to Cube object. In the Start() method we will save both vectors and start time. Finally, the main magic is here in single line of code of FixedUpdate() method. We need calculate magnitude of orthogonal vector using classical formula of periodical vibration: y = a * sin(w*x), where a - is amplitude of vibration, w - is frequency of vibration, x - is time in our case. After that we will perform addition of two vectors and assign to Rigidbody.



That's all! You can download all sources from Github.

6 comments:

  1. I appreciate you to share such news which is helpful for a developer who has to develop new business website.I am not interested in it because I have been marketing for dissertation writing service

    ReplyDelete
  2. Those looking for someone asking to " Do my assignment" can reach out to our dedicated team of experts who have relevant qualifications for writing your assignments. We have extensive knowledge and use of experience in providing assistance on various subjects and topics with well-structured content and completely referenced papers.

    ReplyDelete
  3. https://www.bindia.dk/indisk-take-away.html Ok mad men ikke noget særligt. Vi fik rejer, lam og kylling. Dyre drikkevarer. F.eks. koster en lille mango lassi 50 kr. og en chai the det samme - den var i øvrigt ikke særlig godt. Betjeningen sød og opmærksom.

    ReplyDelete
  4. Getting Psychology Homework Help is the ideal solution for any problems that students may have. Numerous advantages make it one of the most excellent options because it assists students in various ways.

    ReplyDelete