I am making an IOS game in Swift with Spritekit, I have a player Sprite which I want to rotate and move towards where a touch is on the screen. Currently I get the angle, Create the action to turn, Run the action and do the same for the movement. This works well for a single touch, However I now want to do the same when a touch moves. First I tried removing the action then running the new one, The sprite jitters or does not move at all, because the action is being cancelled very soon after being created. I have also tried running it every 100ms however I still do not get smooth movement.
So I was wondering is there any way to modify an action as it is running? Or what is the right way of doing this?
You can override the didEvaluateActions method in your scene class, this gets called after all actions are performed in an update frame. In that method, destroy the old action and start your new action. If you are still seeing jitters, then you need to reevaluate when you want to be removing actions
Related
Using the "new" Unity Input System.
I have a start scene with a menu and from there I can load the game scene where I have a TouchManager, the touch manager allows me to control the player and to visualize the force applied to the player.
To visualize the force I have a game object that starts disabled and with the touch manager I enable the visualizer only when there is touch input.
The issue is that at start of the game scene the touch visualizer is enabled, although after the first touch it starts working perfectly.
With debugging I see that the event that signals "touch" is fired (without touch) but the event that signals the release isn't.
Regarding the code there is nothing particularly relevant as far as I am aware, so I briefly explain: (Scripts by order of execution)
GameManager:
Awake() the game is set to pause;
Start() the game is set to "unpause";
TouchManager:
Pause(bool isPause) input events are subscribed and unsubscribed;
Move is the input that is causing the issue.
I tried to disable the visualizer but has to be on update since the event that enables it is triggered after start method and I dont know how/when the touch event is triggered, also I would like to fix the issue at the source instead of covering up.
Any ideas?
enter image description hereso im basically new to blueprints and i am trying to make a game based on collecting trophies and i finished the code, but for some reason when i drag and drop the trophy it disappears, that wasnt an issue before, im also trying to randomize where the object spawns and i was succesful but after i made changes to the trophy code, suddenly it wont spawn at all, here are some screen shots. enter image description here
As I see you are destroying object on overlap, right before you respawn the object, you should destroy the trophy only after you spawn new, so destroy function must be called at the end, when other logic is finished.
And the best way to handle such operations is some kind of manager. You may use game mode, level, player controller as trophy manager, anything that will exist from the beggining of play and till the level is closed.
I'm trying to make a bow and arrow game. I downloaded a bow that has an animation(?) on it where the string is pulled back.
See:
I want to make a script that triggers that animation (shown on bottom-right of my gif) when Player left clicks. But I don't know how to reference the animation.
Is it something like _anim = GetComponent<Animator>(); and then animation.Play? I cannot tell what the name of the animation even is to do this.
Further.. it would be awesome to be able to control the animation's length depending on how long the user has held down the button, rather than playing it in full even if user only taps left click. I'm not sure how that would be achieved?
Ok, the question seems short but the solution will be long and you need to do a bit of learning. So, let's go through your question step by step.
I downloaded a bow that has an animation(?)
Yes, it is a 3D bow model with an animation in it.
I want to make a script that triggers that animation (shown on
bottom-right of my gif) when Player left clicks. But I don't know
how to reference the animation.
In order to trigger that animation, you need an Animator Controller attached to the bow model instance. Then you can click that Animator Controller to open its panel. After opening the panel, you can simply drag and drop your animation there. When you first drop your animation, it will be the default animation state. However, if you don't want to trigger the animation immediately, I would suggest you to create an empty state and make a transition to the bow animation.
Is it something like _anim = GetComponent(); and then
animation.Play?
More or less like this, but you should check the documentation for better understanding, this documentation explains it well and has a really good example for your use case.
I cannot tell what the name of the animation even is to do this.
Once you drag and drop the animation to the panel, you will see the name of the animation to use in the script.
Further.. it would be awesome to be able to control the animation's
length depending on how long the user has held down the button,
rather than playing it in full even if user only taps left click.
I'm not sure how that would be achieved?
There are many ways to play with animations, for example, you can set an animation parameter to stay in the animation state. Furthermore, you can also disable the Has Exit Time value from the transition to make it stop the animation immediately after the user stops holding the button.
Overall, when it comes to animations, Unity is quite powerful and my suggestion would be read the documentation and watch a couple of tutorials.
Hope this helps.
I have a player with a few parts (arms, hands, weapon) and whenever the player changes move mode (running, standing, aiming), all actions on these parts are removed and new ones are run.
Now, when the player is firing, i run a "shooting" action on the weapon node, and when the player stops firing, i run weapon.removeAction(forKey: "shooting")
The problem is: this "shooting" action obviously gets removed when the player changes move mode (as the weapon's actions are all removed).
Is there any way to remove all actions EXCEPT (forKey: "shooting") ?
I tried giving all other actions the key "moving", but quickly realized that multiple actions cannot have the same key (it overrides the previous action with that key and that previous action is never run). (any fix for this btw? just curious)
the obvious solution is to run the "shooting" action on a separate node from weapon, but that would be less elegant.
Personally, I would add a Gun Node to your sprite, and put the shooting action on that node so that you could remove all actions
There is no way to enumerate over all your actions, you can remove by key, or remove all, that is it.
If you extract your action, remove all, then add it back it, it will restart your action, which you also do not want.
I am having a problem with a game I am currently developing in Swift. I have blocks that turn static upon hitting each other or the ground and my hero sprite cant go through them so instead he just goes under them when enough force is applied. Obviously I dont want this and simply want him to not be able to move any more left or right when he comes in contact with a block from the right or left side.
Assuming your hero moving is an action you programmed, you can use .stopAllActions() to make him stop moving when he collides with one of the blocks.