- Home /
In place animations using Mecanim and how to script it
Hi, I am a 3D guy so programming can be a little overwhelming at times. So I have a character that I am trying to use in a sidescroller. I have my animations for run, walk, and jump imported into Unity. The way I understand Mecanim is that the animations need to move in the 3d software for them to actually move in Unity as opposed to doing in place animations (which I am currently using). I have been searching all over the place and haven't found any good info on how to use in place animations with the benefits of the Mecanim states and blending and how to script this. If anyone has any resources or suggestions, it would be much appreciated before my wall gets a hole from beating my head against it. Thank!
The Unity website has some great tutorials - http://unity3d.com/unity/animation
Answer by 0tacun · Jul 24, 2014 at 04:32 PM
You can easily use Mecanim for in-place movement.
I assume you already have a movement script which lets your character move through the level.
What you need is to expand your movement script with the following (in JS):
//1. we need access to the animator component which is responsible for mecanim, so declare as a variable:
private var myAnimator : Animator;
//2. we need to let myAnimator know whom animator component we should use, so in Start():
function Start(){
//...
myAnimator = transform.GetComponent("Animator");
//...
}
//3. Now we can access the mecanim variables for example in Update() according how we move:
function Update() {
//move player with rigidbody.addforce or however you do
//now check in which state the player is and change the mecanim variable:
//for example if our rigidbody has velocity play walk animation:
if( rigidbody.velocity.sqrMagniture != 0 )
myAnimator.SetBool("isWalking", true);
else
myAnimator.SetBool("isWalking", false);
}
And don't forget to properly set up the mecanim states:
Otacun, thanks! That is the direction that I am looking to go with it. I understand what is happening here. I am missing the movement script at the moment. I have a couple I have been messing with but have not successfully figured out how to get the player moving around while playing the animations through $$anonymous$$ecanim.
Good luck then! You can do it, try to implement my framework I provided into your movement scripts.
If my answer was sufficient, please click "accept answer" :)
I think that gives a good place to start. Would you happen to have any reference on the movement and force to rigid body? If not, again I have an idea where to start now.
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
The documentation is your deadliest weapon.
Answer by Xtro · Jul 24, 2014 at 03:54 PM
As a developer, I must say that, if you are a 3d guy, you should find a developer. I don't try to make 3d models since I'm a developer.
Don't get me wrong. Learning new stuff(including programming) is good for a person. The problem is, if you are into programming, you can't start it with complex stuff like game, animation, unity or mecanim scripting. You should begin with the very basics outside of Unity.
I'm not a beginner at program$$anonymous$$g in Unity. I have been doing C# for the past couple years on various projects. Just some of the more complex calls and referencing systems daunt me a little bit. I do 3D, GUI program$$anonymous$$g, animation program$$anonymous$$g, light baking, yada yada on a daily basis. But generally I am able to find reference on things I am not entirely sure about.
Actually should probably specify that the work I do in script is generally a lot of animation calls a GUI button presses so not a lot of character controlling.
I'm sorry if I offended you. I wrote it because you said "I am a 3D guy so program$$anonymous$$g can be a little overwhel$$anonymous$$g at times."
No, not at all. I should have specified. I am primarily function as a 3D artist but do some program$$anonymous$$g also but sometimes I have trouble wrapping my head around different things so I go through a lot of reference and a lot of tutorials as well as consult with programmers on methods.
Your answer
Follow this Question
Related Questions
Possible to play single clips using Mecanim? 0 Answers
For easy simple games, you can do without using Mecanim and instead just do it through Scripts? 1 Answer
Mecanim scripting interface 1 Answer
Is it possible to manipulate Mecanim state machine / blend trees from scripting? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers