- Home /
Using iTween for a sine-wave motion
Hi all, I'm new to iTween but I would really like to create a sine motion for an object.
It's for a shoot'em'up game, and I want my projectiles to move up and down on the y axis and to keep moving forward along the x axis.
I don't want to use paths, because they are restricted to 10 nodes and as far as I can tell, there is no way to move an object relative to a path (they always start at the position of the start node of the path, which is not what I want). So I'm trying to set up tweens.
I've tried to add two "MoveBy" tweens, one with a pingpong loop along the y axis, and one with no loop along the x axis. However it seems you can't combine tweens of the same type.
Is there a way to achieve this in iTween at all, or do I have to code all this manually?
Answer by robertbu · Jun 12, 2014 at 08:19 PM
I don't want to use paths, because they are restricted to 10 nodes and as far as I can tell
This is not true. As far as I know, the only limitation on paths length is available memory.
But if you did not want to use a path, then write a simple script that moves your object up and down in a sine motion using local coordiantes. Then make your visible object a child of an empty game object. The empty game object is what you use with iTween.
Here is a simple script that moves an object up and down on the its local position in a sine movement:
#pragma strict
var max = 2.0;
var speed = 2.0;
function Update () {
var f = Mathf.Sin(Time.time * speed) * max;
transform.localPosition = Vector3(0.0, f, 0.0);
}
I guess you can programmatically set as many nodes as you can for a path, but in the visual editor, the "Node Count" slider is restricted to 10 as a maximum.
Thanks for that workaround, I didn't think of using two separate objects, pretty clever :-)
$$anonymous$$eanwhile I had come up with my own solution:
public class SineWave$$anonymous$$oveScript : $$anonymous$$onoBehaviour {
public float speed = 10.0f;
public float amplitude = 1.0f;
public float frequency = 0.0f;
private float previousOffset = 0.0f;
private float refAngle = 0.0f;
void Start() {
}
// Update is called once per frame
void Update () {
// The base idea: use a reference angle that is increased according to frequency, and use its sine value to calculate the motion on the y axis.
// we update the reference angle according to deltaTime and the frequency
refAngle += 360*Time.deltaTime*frequency;
// we make sure the angle is never > 360
refAngle = $$anonymous$$athf.Repeat(refAngle, 360);
// we calculate the new offset of the gameobject on the y axis
float yOffset = ($$anonymous$$athf.Sin(refAngle*$$anonymous$$athf.Deg2Rad) * amplitude);
// we move the gameObject on the axis
transform.Translate(Vector3.up*(yOffset-previousOffset));
previousOffset = yOffset;
// we use the "right" axis to move the gameobject on the x axis
transform.Translate (Vector3.right*speed*Time.deltaTime);
}
}
I haven't played with the node editor in forever, but did you try and increase the node count in the Inspector? $$anonymous$$y guess is that it defaults to 10 nodes, but you can reset the value to any value in the Inspector.
I tried to manually set the node count to 12 in the inspector, but it got reset to 10 as soon as I hit enter.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151347im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iTween sine wave like movement 1 Answer
Stop iTween destroying all tweens 0 Answers
Itwen MoveTo - jump 1 Answer
What is the difference of iTween and Waypoint? 0 Answers
iTween.RotateAdd Gives Wrong Results 0 Answers