How do i rotate an object in unity at a set of amount time stop it and then again - unityscript

I'm not good at coding so please help me with my problem.
What i mean with: How do i rotate an object in unity at a set of amount time stop it and then again
is how can i rotate an object at a set of amount time then stop it at a set of amount time then stop it etc.
VideoNations

Easiest way would be like this:
// control the timing of rotating here
var rotate=true;
function Start () {
// rotate an object for 2 seconds
rotate=true;
yield WaitForSeconds(2f);
// stop it for a second
rotate=false;
yield WaitForSeconds(1f);
// rotate again
rotate=true;
}
// rotate here
function Update() {
if(rotate)
transform.eulerAngles+=Vector3(0, Time.deltaTime*90, 0);
}
If you want the rotation enabling-disabling to loop, wrap the contents of Start in a while(true) {...}.

Related

Multiple overlapping animations on the same element in svg.js

I want to start a animation on an element while a previous animation is still active. However, calling animate() on the element queues the new animation at the end of the current animation.
For example, consider an animation where an element is being moved to a new position. Now, I also want to make it fade out when it reaches a certain position. The following queues the “once” animation at the end of the move, rather than at 80%.
rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
rect.animate(200).opacity(0);
});
How do I make the element start fading out when it reaches 80% of the move? The API seems to be designed for chaining animations rather simultaneous overlapping animations.
What you are trying to do is a bit more complicated. Unforrtunately its not possible to "hack" into the current animation and add a new animation on the fly.
However what you can do is adding a new property which should be animated:
var fx = rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
fx.opacity(0);
});
As you will notice that has its own problems because the oopacity immediately jumps to 80%. So this is not an approach which works for you.
Next try: Use the during method:
var morh = SVG.morph(
var fx = rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
fx.during(function(pos, morphFn, easedPos) {
pos = (pos - 0.8) / 0.2
this.target().opacity(pos)
}
});
We just calculate the opacity ourselves

Slowly move a circle in PIXIJS

I have a circle with PIXI and I need that when the touch is fulfilled another object said circle returns to its original position, but it happens that I do not know any function or method for the movement to be appreciable, all I have achieved is that it disappear and appear in its initial position. How did the transition happen?
Just add the movement into rendering loop
I use PIXI.tickerTicker() for loop
let ticker = new PIXI.ticker.Ticker();
the rendering loop in your situation should be
function loop(){
renderer.render(stageContainer);
yourCircle.position.x += moveSpeed; //your question
if(resetCirclePosition)
yourCircle.position.x = defaultPos;
}
and then to start ticker use
ticker.add(loop);
ticker.start();
Look at ticker documentation http://pixijs.download/dev/docs/PIXI.ticker.Ticker.html

ILNumeric continuous rendering plots

Is there a way to continously plot changing array data? I've got a ILLinePlot to graph the line to changing data on a button event, but I would like to make it continuous.
while (true)
{
float[] RefArray = A.GetArrayForWrite();
//RefArray[0] = 100;
Shuffle<float>(ref RefArray);
Console.Write(A.ToString());
scene = new ILScene();
pc = scene.Add(new ILPlotCube());
linePlot = pc.Add(new ILLinePlot(A.T, lineColor: Color.Green));
ilPanel1.Scene = scene;
ilPanel1.Invalidate();
}
The problem I'm running into is that the loop runs, and i can see updates of the array, but the ILPanel does not update. I'm thinking maybe it's because the main loop can't be accessed due to this infinite loop, so I put it in its own thread as well, but it's still not rendering as I hoped...
As Paul pointed out, there is a more efficient attempt to do this:
private void ilPanel1_Load(object sender, EventArgs e) {
using (ILScope.Enter()) {
// create some test data
ILArray<float> A = ILMath.tosingle(ILMath.rand(1, 50));
// add a plot cube and a line plot (with markers)
ilPanel1.Scene.Add(new ILPlotCube(){
new ILLinePlot(A, markerStyle: MarkerStyle.Rectangle)
});
// register update event
ilPanel1.BeginRenderFrame += (o, args) =>
{
// use a scope for automatic memory cleanup
using (ILScope.Enter()) {
// fetch the existint line plot object
var linePlot = ilPanel1.Scene.First<ILLinePlot>();
// fetch the current positions
var posBuffer = linePlot.Line.Positions;
ILArray<float> data = posBuffer.Storage;
// add a random offset
data = data + ILMath.tosingle(ILMath.randn(1, posBuffer.DataCount) * 0.005f);
// update the positions of the line plot
linePlot.Line.Positions.Update(data);
// fit the line plot inside the plot cube limits
ilPanel1.Scene.First<ILPlotCube>().Reset();
// inform the scene to take the update
linePlot.Configure();
}
};
// start the infinite rendering loop
ilPanel1.Clock.Running = true;
}
}
Here, the full update runs inside an anonymous function, registered to BeginRenderFrame.
The scene objects are reused instead of getting recreated in every rendering frame. At the end of the update, the scene needs to know, you are done by calling Configure on the affected node or some node among its parent nodes. This prevents the scene from rendering partial updates.
Use an ILNumerics arteficial scope in order to clean up after every update. This is especially profitable once larger arrays are involved. I added a call to ilPanel1.Scene.First<ILPlotCube>().Reset() in order to rescale the limits of the plot cube to the new data content.
At the end, start the rendering loop by starting the Clock of ILPanel.
The result is a dynamic line plot, updating itself at every rendering frame.
I think you need to call Configure() after any modification of a shape or its buffers. Use the BeginRenderFrame event to do your modifications and you should not add infinitely many shapes / new scenes. It is better to reuse them!
Let me know, if you need an example...

Animation synchronisation in Three.js with Tween.js

I'm trying to animate sprites along a path in Three.js using Tween.js by chaining the animations in order to have something like this :
----#----#----#----#----#---- etc
Every sprite has its own tween animation, and I just delay each tween animation at the beginning. Each sprite has in fact N animations along the path (that is not a straight line) and I chain them to have a loop effect.
Everything goes well if the FPS is perfectly stable, but my problem is that if at some point I have a FPS drop, the animations of the different sprites are not in sync anymore, and the space between sprites is not equal anymore. I potentially end up with something like this :
---#--#----#-#-#-----#--- etc
I was wondering if there is a better approach for this, like having only one tween animation for all the sprites, but I don't know how to introduce the offset between each sprite on many line segments.
I cannot post the exact code has it is part of a bigger app, and won't be usable as is, but it looks like this :
// create animations
for each (sprite) {
for each (segment) {
var currentAnimation = new TWEEN.Tween(sprite.position).to({
x : segment.endpoint.x,
y : segment.endpoint.y,
z : segment.endpoint.z
}, animationTime).easing(TWEEN.Easing.Linear.None);
currentAnimation.delay(delayTime * currentSpriteNumber);
previousAnimation.chain(currentAnimation);
}
lastAnimation.chain(firstAnimation);
lastAnimation.onComplete(onEachSpriteAnimationCompleted);
}
// start the animations
for each (sprite) {
spriteFirstAnimation.start();
}
// to remove the delay when each sprite animation has made one loop,
// and instantly replace the sprite at the beginning of the path
// (my paths are not closed)
var onEachSpriteAnimationCompleted = function() {
sprite.position.set(starting position);
for each (sprite animation) {
animation.delay(0);
}
}

Unity 3D Spinning a gameobject

I am trying to spin a 3D Gameobject in unity. It is actually a cylinder with just a texture of a poker chip on it. I want to spin it 360 degrees once it has collided with a raycast. It is working just fine in the Unity emulator, however, on the device itself the chip comes to a stop after its spin and then continues to spin in an endless loop. Here is a snippet of the code in question. Thank you for any help in advance.
// Spin the chip
if (Animate) {
if (Speed > 0 && Chip.tag.Contains("Chip")) {
Chip.transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= 3;
Debug.Log(Speed);
}
else {
// Reset
Animate = false;
Speed = 360;
Chip.transform.localRotation = Quaternion.Euler(0.0,0.0,0.0);
}
}
To summorize this the best I can the gameobject Chip is assigned when it collides on raycast as such
// Set the chip
Chip = hit.transform;
Everything is done in the update function. Once the raycast hits it calls a betting function then after the betting is calculated it changes the Boolean Animate to true causing the spinning of the chip.
Something is setting Animate = true in some other code, hard to tell whats going on without seeing the rest of it.
Put some debug next to every spot where Animate is set to true, you should see something else setting it, only possible explanation as to why it continues to spin.
Another option is to use the Animation tool and instead of rotating, you just play the animation which performs the rotation for you.
Edit: Chances are its around the touch code, cause when you debug in the editor your using key strokes. A gotcha I've experienced a few times.
James Gramosli is correct in that some other code is triggering the animation again and it is most likely your touch code. It is a common problem when moving between editor and a touch-enabled device. You can determine if this is the case by using the UnityRemote to verify the control flow of your code.
That said, I would change your code to the following which removes the spin code from the Update loop that runs every frame. It is a small optimization, but primarily it cleans up the architecture and makes it more modular and a little neater.
It is not clear from your code snippet, but I will assume you are using UnityScript.
In your script that handles the touch code when you click on the chip, insert this line:
hit.transform.SendMessage("Spin", hit.transform, SendMessageOptions.DontRequireReceiver);
Put this code in a separate script called "SpinChip" and then add the script to your chip object.
var StartSpeed = 360.0;
var Deceleration = 3.0;
function Spin()
{
if (Animating)
{
print("Chip is already spinning, not starting another animation");
return;
}
/*
This code isn't necessary if this exists in a separate script and is only ever attached to the clickable chip
if (!gameObject.tag.Contains("Chip"))
{
print("That wasn't a chip you clicked");
return;
}
*/
print("Chip has been told to spin");
StartCoroutine(SpinningAnimation);
}
function SpinningAnimation()
{
print("Chip spin start");
transform.localRotation = Quaternion.identity;
Speed = StartSpeed;
Animating = true;
while (Speed > 0)
{
transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= Deceleration;
yield; // wait one frame
}
print("Chip has completed the spin");
Animating = false;
}
What this code does is create a co-routine that runs once per update loop when activated that will spin the chip for you, and is independent of your actual button clicking code.
var rotSpeed: float = 60; // degrees per second
function Update(){
transform.Rotate(0, rotSpeed * Time.deltaTime, 0, Space.World);
}
Here is a code that rotates your game object, you can use it just with a vector 3 :transform.Rotate(x, y, z); or with Space transform.Rotate(x, y, z, Space.World);
rotSpeed is the rotation speed.
In your update function . The Bool variable Animate may becoming true . This may be reason your cylinder continues to rotate.
Other Solution is : You can create an animation of your cylinder and then take a stopwatch . So that after sometime you can stop you animation using the time of stopwatch

Resources