Hi all,
I’m trying to learn JS in Unity, specifically trying to learn character controls at the moment. I made a swordsman toon and got him attacking, moving and attacking while moving. He currently has an idle while standing but I’d like to add a kneeling or crouched posture idle. I’d like him to go into a kneeling position when a button is pressed and stay in that position, go into an idle and/or be able to do my strikes. Stand when another key is pressed.
The script I’m using:
private var walkSpeed : float = 1.0;
private var gravity = 100.0;
private var moveDirection : Vector3 = Vector3.zero;
private var charController : CharacterController;
function Start ()
{
// Set all animations to loop
animation.wrapMode = WrapMode.Loop;
// except striking
animation[“backhand2”].wrapMode = WrapMode.Once;
animation[“forehand2”].wrapMode = WrapMode.Once;
charController = GetComponent(CharacterController);
// Put idle and walk into lower layers (The default layer is always 0)
// This will do two things
// - Since strike and idle/walk are in different layers they will not affect
// each other’s playback when calling CrossFade.
// - Since srike is in a higher layer, the animation will replace idle/walk
// animations when faded in.
animation[“backhand2”].layer = 1;
animation[“forehand2”].layer = 1;
// Stop animations that are already playing
//(In case user forgot to disable play automatically)
animation.Stop();
}
function Update () {
// Based on the key that is pressed,
// play the walk animation or the idle animation
if (Mathf.Abs(Input.GetAxis(“Vertical”)) > 0.1)
animation.CrossFade(“shuffle”);
else
animation.CrossFade(“mididle”);
// Strikes
if (Input.GetButtonDown(“Fire1”))
animation.CrossFadeQueued(“backhand2”, 0.1, QueueMode.PlayNow);
// Srikes
if (Input.GetButtonDown (“Fire2”))
animation.CrossFadeQueued(“forehand2”, 0.1, QueueMode.PlayNow);
// Calculate the movement direction (forward motion)
moveDirection = Vector3(0,0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
}
I’m really new to scripting and the above was copy pasted/mashed together from various tutorials that I’ve been studying. I tried adding another If else line but It either loses the movement, just kneels and go back to standing when striking or breaks it entirely.
Any help on how to add a kneeling idle to the above or links to JS formatting would be greatly appreciated.
Thanks,
K3D