hey everyone, im a 3d modeler but pretty new to unity. ive looked around at a bunch of tutorials, but am struggling to just get this part right.
ive done the tutorial where a guy uses the third person script and 3rd person animation script from the platform tutorial, but i would rather know how to script my own character controls and not be limited to that template.
all i want for now is the basic format for example
if the user presses W, then the character goes forward and plays walk animation.
if im right i feel the format should be able to be easily editable for other animations, i just cant get it to work 8(
any help anyone?
Hello,
so you don’t want to use the built in character Controller ?
I can understand very good - it is nice for the beginning, but you’ll want something more customizable.
No Problem. If you have a rigidbody attached,
you would simply manipulate the velocity, or add a custom force.
If physics are to be ignored (without rigidbody) you would alter the transform.position directly.
Here is exactly what you asked for:
(Needs rigidbody attached. Turn off Gravity if you don’t have a platform under you that is solid)
if (Input.GetKey("w"))
{
Debug.Log("Moving Forward");
rigidbody.AddForce( (transform.forward * Time.deltaTime * 3) ,ForceMode.Acceleration);
if (!gameObject.animation.isPlaying)
{
gameObject.animation.Play("YourAnimationNameHere");
}
}
this code has to be in the update function of the thing that’s gonna be controlled.
So just open a new java script and paste my code inside the update function.
Another beginner mistake is to forget to attach the script to the object. You don’t believe how many hours i’ve spent to look why my scripts don’t work until i found out that i forgot to attach them to , for example, my new prefab.
Hope that helped you to get a good start 
Kind Regards,
David
hey thanks for the help… i tried it but couldnt quite get it workin properly?.. im in the middle of a different method but running into a different problem.
this is what i have set up so far,
i made a FPS prefab in the scene and deleted the camera, and made a new one behind my character
i took the Graphics, and removed the mesh, and then aligned my character within the Controller for the FPS.
i then put my character prefab under the Graphics in the heirarchy, so it follows the movement.
… so now what im doing is setting up the animation for the character on keycodes… this is the code im using right now. the problem is that when the character starts the “run” animation when i press W, but when i release W the “run” animation keeps looping instead of going back to “idle”
function Update () {
if (Input.GetKeyDown(“w”))
{
animation.wrapMode = WrapMode.Loop;
animation[“run”].layer = -1;
animation[“idle”].layer = -2;
gameObject.animation.Blend(“run”);
}
else
{
gameObject.animation.Blend(“idle”);
}
}
Maybe try calling the idle animation on GetKeyUp?
i tried that before… idk if i did it wrong but it just seems to be ignoring the else, or key up.
i think when i had it, it looked somethin like this. basicly the same as down but i changed it to Up, and Idle animation.
function Update () {
if (Input.GetKeyDown("w"))
{
animation.wrapMode = WrapMode.Loop;
animation["run"].layer = -1;
animation["idle"].layer = -2;
gameObject.animation.Blend("run");
}
{
if (Input.GetKeyUp("w"))
{
animation.wrapMode = WrapMode.Loop;
animation["run"].layer = -1;
animation["idle"].layer = -2;
gameObject.animation.Blend("idle");
}
}
is there somethin wrong in there?
I’m still pretty new to Unity and animation control so I’m gunna just be guessing mostly. Did you try reversing the layers for the keyup function?
If that doesn’t work, try this:
if (Input.GetKeyDown("w"))
animation.CrossFade("run");
else
animation.CrossFade("idle");
You might still need to include the wrap layers, define them outside the function and put them both on -1.
So this would go above the Update() function:
animation.wrapMode = WrapMode.Loop;
animation["idle"].layer = -1;
animation["run"].layer = -1;
animation.Stop();