i have a sprite & a set of coordinates... I want my sprite to follow that path & its following but its so fast that i can see only the last position......
So i want to introduce delay in every new position.. so how i can achieve this
Short ansfer: NSTimer. Update your sprite position on every timer tick. Generally, you want to have a master timer/clock that fires X times per second and update all your animation logic based on that clock. 30fps is good for games.
If you're doing a game, have you checked out cocos2d-iphone?
Related
I'm currently working on an FPS game and I have these hand models as well as weapons. But I want it so that if I press the W key (move forward) it plays the sprinting animation with the arms. I initially thought of coding it like, if(Input.getkeydown(keycode.w))animation.play or something like that. But I'm using the character controller which doesn't use that. It uses the Input.getaxisraw. How should I implement this with the character controller?
Thank you a ton in advance
You will need to implement a basic animator controller. You can define all your states (Idle, Crouch, Jump, Sprint, Aim) and define the transitions between them. You may then add parameters like floats, triggers and booleans as conditions to transition between these states.
In the above example, if you want to move to a crouch state, you would use
GetComponent<Animator>().SetBool("Crouch", true);
Or for another example, if you want to move from Run to Walk. The transition (The blue lines indicating the movement from one state to another) will have a condition to move from walk to run and back based on the float parameter "Speed". if Speed>x, move from walk to run. else move from run to walk. In this case you would set
GetComponent<Animator>().SetFloat("Speed", floatValueHere)
and the controller will take care of the animation transitions.
This is just a pointing to the right direction. you will need to watch a couple of videos on animator controllers Check Unity's simple 8 minute explanation here Hope this helps!
I have a 2D sprite animation of a waving flag, which I'm playing in a loop.
The problem I'm having is that the Unity Animation Editor is making the last sprite of my animation play for only one frame, whereas I have every other sprite playing for several frames. So when the animation loops back to the beginning, the last sprite transition doesn't look smooth because the last sprite is visible for fewer frames than every other sprite.
Is there a way to increase the number of frames that my last sprite plays for in the Animation Editor? Or is there a different way to handle this problem?
Here's my animation. I circled where the problem is.
Click on the diamond above 0:00 and copy. Then go to 0:24 and paste it. First and last sprites look the same but if not that is the reason for this and what I propose must work.
Note: BugFinder's solution works as well:
adding a keyframe at the end without changing anything is not the same as an empty one..... The sprite value remains the last one
I have the following sprite animator:
I want to reproduce all 10 sprites on first loop, but on second loop I want to start at sprite number 6. That's because the 5 firsts sprites are from make a good transition to walk to run, but once zombie is runing these sprites look weird to repeat.
Is it posible to do?
As per usual there are many ways to solve this problem. It seems like the easiest way to solve it would be to have three walking animation states. OnStart => Walking => OnStop. Where OnStart and OnStop play a single time. Use all 10 sprites for your on start animation and then use sprites 6-10 for your walking animation. So when your character starts to move it plays the OnStart animation once and transitions straight into the walking animation. For extra credit you could make a stopping animation where similarly when the character stops it plays a one-shot unique animation of the character coming to a stop. If you are attempting to have the complete spectrum from stationary=> walking=> running my suggestion would be to research how to use blend trees. Basically you will end up having an "idle","walking", "running" animation and based on the speed of the character unity will blend those animations together. But my first suggestion would be the simplest.
I was wondering how to have it in Cocos2D to make a sprite move up, while the user's finger is being held on the screen. I have a mechanism in place that does so, but only moves the sprite once.
Is it possible to have the sprite move up while the finger is holding on the screen?
Also, even though it's not necessary, if you could make it so when the user isn't holding on the screen, the sprite goes down.
Thanks, I think that a physics engine such as Box2D of Chipmunk is necessary, but I'm not sure how to do this.
-Soule
I have a not so perfect answer:
set up a repeatable timer with a short interval, at most 0.1 second, then fire the timer in ccTouchBegan, invalid the timer in ccTouchEnded. Everytime the timer ticks, move the sprite a short distance.
the shorter the interval is, the smoother the move will be.
I am developing an iPhone version of this game. http://www.students.uni-mainz.de/rathb000/Fillit/Game.html
I am using an NSTimer to change the position of the ship by 1 pixel and the time interval for the timer is 0.002.
However, i have noticed that the ship doesnt move faster if i reduce the time interval furthur. Infact, there is no difference between 0.05 and 0.002. Is there is a restriction on the minimum time you have to give as NSTimeInterval?
The ship currently moves too slow for the game to be exciting.
Also, the movement of birds (which is also achieved by NSTimer) becomes slow when i press any button for moving the ship.
Any idea about how this can be solved?
NSTimer is not a real-time mechanism; it fires as part of the run loop and is not ideal for creating smooth, fluid animations. However, there is an SDK that is part of the iPhone OS called Core Animation - it provides a library of functions for performing smooth animation of layers and views. Here is a reasonably good series of video tutorials that you may find useful. There is also an excellent book called Core Animation for Mac OS X and iPhone that you may find useful.
There is also the core animation documentation from Apple that describes how to use the CABasicAnimation class to animate properties of any layer or view. Here is an example of an explicit layer animation from the docs:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:#"opacity"];
theAnimation.duration=3.0;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=CATransform3DTranslate(0,0,0);
theAnimation.toValue=CATransform3DTranslate(20,20,0);
[theLayer addAnimation:theAnimation forKey:#"animateTransform"];
From NSTimer docs:
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs while the run loop is in a mode that is not monitoring the timer or during a long callout, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
Maybe you can try moving the UIView using UIView animations instead of a timer.
You could consider increasing the distance size: move the ship 3 or 4 pixels every .05 seconds rather than 1 pixel every .002 seconds.
In my opinion, you should be using the same timestep globally across all entities in your game, not a timestep exclusively for the player's ship. If the ship is moving too slowly with your timestep, increase the amount of distance it is moving per frame.
Really, you should be thinking of moving your player's ship in terms of pixels/second. When the NSTimer event fires, get the elapsed time since the last update. Use this elapsed time to move your game entities. For instance, if your update timestep is 0.05 seconds and you want to move the player's ship 100 pixels per second, then you should move it 100 * 0.05 = 5 pixels in this update. This method will account for varying size timesteps and your game entities will always appear to moving at a consistent speed, even if your timer mechanism is not that consistent.
Not only is NSTimer not the right tool for this type of animation your design is wrong.
A timer with an inteval of 0002 fires 500 times per second. That's a crazy way to animate this game.
I suggest you read up on 2D game design and find out a better way to do collision detection - like anonymous suggests you should have a collision rectangle round the ship rather than relying on the actual pixels.
You should also check out Cocos-2D for iPhone
Figured this out.
Had to use Core Animation to move the ship and made the birds move by NSTimer. Works fine this way but only upto a certain limit. Birds again slow down when the speed of the ship increases more than a certain limit.