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.
Related
DESCRIPTION OF THE PROBLEM
I have a character with a throwing animation that has a Rigidbody with "Use gravity" checked. At the moment of the throw, in the middle of the animation, the character moves up a from the ground.
OBSERVATIONS ABOUT THE PROBLEM
This upward movement does not seem to be present in the animation itself when viewed in Unity's animation inspector, nor is it present in Mixamo, which is where I got it from.
The character never comes down if I DO NOT check the option below and keeps climbing up each time the animation is played.
If I DO check it, then the character STILL goes up but immediately comes down once the animation is finished.
If I check "Freeze position (Y)" as below:
The problem stops but other animations such as death animations stop working as the character floats after dying.
I don't think it's the collider because the character stays well up off the ground after the animation ends and keeps going up more and more each time, staying far away from the ground.
The problem persists with "Apply root motion" unchecked in the Animator.
WHAT I HAVE TRIED
Disabling all scripts, removing the animation event that was present and fiddling with all of the settings that I mentioned here, to no avail.
QUESTIONS
1) Why could this be happening? How could I find the source of this movement given that I don't see it in Unity's animation inspector/Mixamo?
2) Is there a better fix than creating a StateMachineBehaviour script with OnStateEnter and turning on the Y constraint there and turning it off in OnStateExit?
I had the same problem as you.
In my case, the problem came from the definition of the NavMesh from a Plane. For some unknown reason, Unity created the NavMesh plane with a little elevation. Finally, I changed the plane to a cube and the navmesh succeeded, so the agents move well on zero height, as expected.
After many hours reviewing the animations and the different options, the problem was in the generation of the NavMesh.
Hope this can help you.
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'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.
My game main player is sometime moving and some time in game play remain at same position. I have added trail renderer as child gameobject but when player is moving only that time trail get drawn as player stop its movement trail slowly become invisible.
This thing I don't want, I want always at least some trail get drawn with player. But how to do this?
To make the trail persist, increase the "How long does the trail take to fade out." docs - trailRenderer time
Now the trail stays longer/forever. It won't vanish when you move a lot, too.
Maybe you want distance-vanish instead of time vanish.
You can iterate over the points and compare distances. then dissolve them.
I think what you need is a particle system with the trail renderer module activate. For the effect you showed you could make it sending a single particle in the downward direction and using a trail to follow it.
Edit: Here is an image showing where the module is. You will need to tweak a little while to get your desired effect but it looks doable. Have fun!
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.