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!
Related
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'm trying to create my first computer game and I'm finding myself spending a lot of time trying to make the transitions between the character animations.
I am trying to make a transition from Jumping to Shooting (which have a different animation). I am making the transition from "Any State" to jumping based on a boolean parameter which is true whenever the character is in the air and I'm making the transition to shooting based on a Trigger paramter. Whenever I'm shooting with my character while in the air, it shows the shooting animation for a moment but make the transition back to the jumping animation before finishing it (I assume because it uses the "Any State" to make the transition back to the jumping animation and not the transition between "ShootHorizontalJump).
How can I make the shooting in air animation finish before going back to jumping?
Thank you for you help :)
My Animator
If you are using an animated Sprite Sheet as an animation, You can not play two animations at the same time.
Otherwise, You can use Animation Layer. It's same on 2D and 3D. Check here.
As title suggests I am working on hand to hand fighting system. Now its just a start and I am also not professional so I have setup moves of players and enemy.
Now problem occurs when player hits enemy, it should trigger enemy "taking hit" animation. Now it triggers but both animation of "hitting" and "taking hit" at same time,so it doesn't look at it is meant to be.
So please anyone can set me on right path of what to do for like starting of "taking hit" when "hitting" animation reaches at some key frame or any other way.
I'll post code if you require but only a suggestion or link to some documentation will do.
Thanks
Probably the code is not the problem here. You should detect when the other player hits you, then set the parameter in the animator like this:
yourAnimator.SetBool("TakingHit", true);
Then in your animator, you should put the TakingHit animator and configurate the transition when the TakingHit parameter is activated.
Another thing you should considerate is that if you activate the "TakingHit" parameter bool in a loop, it will start playing the animation in a endless mode.
Current Situation
I have a character and a "walking" animation.The animation is used in an animation controller. All assigned and done with the Inspector, no scripts involved. "Loop" is active.
When I hit the "play" button, the character's position moves according to the animation.
Problem
Once the animation reaches the end, the character gets relocated back to the origin point.
Question
How do I achieve that the character continues walking forward?
I read that I should activate "apply root motion" which I did, but that doesn't change anything, the character's position still gets moved back to the origin after the animation ends.
do all your animations in place and move your object via code , change player position with code and let your in place run animation play.
I really dont think that this can or should be done without any scripts.
Even currently playing animation should depend on parameters of the animator, as well as the movement itself.
And you won`t have to write a separate script for every state.
One script can get the user input, assign animator parameters (e.g. if the player had pressed LeftCtrl it sets 'Crouch' parameter to true and animator triggers related animator state/transition) and update character position depending on the speed and direction values.
In order to save some time finding appropriate value of movement speed to match your animation, you can add an animation speed multiplier to every state in the animator and make it a parameter. In that case one variable with speed value will modify actual movement speed and animation framerate.
I've recently started using Unity for a resource management stealth game. The stealth part is turn based, similar to Hitman Go. I have a simple character controller and a simple patrolling AI over a specific path. However, these movements work in real time and I want to change that to turn based. The AI should wait for the player to finish his/her move and then move itself. The same goes for the player.
Both the player and AI should be able to move to their adjacent waypoints only when the movement of the other part is complete.
How should I go about that?
Thank you
The language that I'm writing in is UnityScript.
As a very simple solution, firstly you can create an empty gameobject. Name it as TurnController. With a simple script you can add a boolean variable on it. Lets name it as isPlayerTurn. For player movement you can check this, if it is true player can move. At the end of his/her move (maybe clicking end turn button or when it reachs the max distance to move or something else) you can set isPlayerTurn false. Ofcourse AI should check (Maybe in Update function. But can change by your design) if it is true, AI can do what it needs to do. And at the finish of its turn, it should change isPlayerTurn back to true. I know it is a very simple solution but hope it helps for begining. And I hope I didnt misunderstand your question.
Write the ai as a player instance and have it emulate player input.
(Instead you could also implement a common interface on both classes.)
Spawn a game object with a GameManager behaviour script that stores a reference to the current player (or ai). Then have the GameManager update the current player every frame by checking their input. If the (human) player gives input while it is not his turn, his input will just be ignored.
This way, the player and ai do not have to know if it is their turn.