1
\$\begingroup\$

I want to move my gameobject to move point A to target point B like sine wave, I have used Vector2.Lerp but its just straight line. so far I tried many ways and I managed this

//sine wave
vLastPos = new Vector2(transform.position.x , Mathf.Sin(Time.time)*2f+2f);
transform.position = vLastPos+Vector2.right*Time.deltaTime;

but the problem is its moving the gameobject left to right not to target position. the gameobject shold reach the target position and stop. please help me.

\$\endgroup\$
1
  • \$\begingroup\$ Are you sure that isn't C#? \$\endgroup\$
    – Droppy
    Oct 12, 2016 at 8:40

3 Answers 3

5
\$\begingroup\$

I'm going to explain as much as I can to avoid more questions.

Let's begin from the simple linear motion. It's something like:

X = X + cos(angle)* distance * deltaTime;
Y = Y + sin(angle)* distance * deltaTime;

First of all, we're using trigonometry to find out how much we should move an object at the specific coordinate using Sin and Cos functions to convert it from the angle. Think of it as converting angle to a unit vector by finding projections to each coordinate axis.

enter image description here

Then we're multiplying the result to the distance to move from the origin (speed of the movement, actually) and multiplying to the time that got past from the previous frame to make movement independent from the framerate. Note that this action makes everything being measured in units-per-second. Last thing to explain is the angle, but I think Unity has methods to calculate angle between two points so it shouldn't be a problem.

Well, we got it moving to the desired point, what's next?

Let's modify the angle, here's how to do it:

X = X + cos(angle + sin(Time*speed)*amplitude)* distance * deltaTime;
Y = Y + sin(angle + sin(Time*speed)*amplitude)* distance * deltaTime;

This will make your object move to the desired point with a wavy behaviour, but note that speed will decrease amplitude because we're just rotating the angle and too big amplitude will make the object stay spinning at the same position being unable to move. This is what the resulting pattern looks like:

enter image description here

It is also able to home to your target:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

You can use a parametric sine wave, combined with a rotation matrix equation:

var theta = some angle
var x = t
var y = amp * sin(t)
var xp = (x * cos(theta)) - (y * sin(theta));
var yp = (x * sin(theta)) + (y * cos(theta));
draw(xp, yp);

var cvs = document.getElementById('c');
var cxt = cvs.getContext('2d');

var cw = cvs.width / 2;
var ch = cvs.height / 2;

var theta = 0;
var amp = 10;

function Draw()
{
  cxt.beginPath();
  cxt.moveTo(cw, ch);
  for (var t = 0; t < 300; ++t)
  { 
    var x = t;
    var y = amp * Math.sin(t / 5);

    cxt.lineTo(
      cw + (x * Math.cos(theta) - y * Math.sin(theta)),
      ch + (x * Math.sin(theta) + y * Math.cos(theta))
    );
  }
  cxt.stroke();
}

(function loop()
{
	cxt.clearRect(0, 0, cvs.width, cvs.height);
	Draw();
  theta += 0.01;

	requestAnimationFrame(loop);
})();
html {
  font-family: sans-serif;
}
canvas {
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
}
<canvas id='c' width='300' height='300'></canvas>

\$\endgroup\$
-1
\$\begingroup\$

To add variety to the answers, you could use an AnimationCurve to manually offset a position along a linear path, so you can get any shape wanted.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .