- Home /
Slow down and play walk animation on left shift?
Sorry for this silly queston but I'm not good at coding at all. I have a very simple 3rd Person controller script, many animations, but so far I've only integrated a 'run' and 'idle.' I know almost nothing about Javascript and I don't plan to know more soon especially because my current project requires very little scripting. I know how to add independant animations like 'bark' or 'sit' on keypress, but I'd love to know how to reduce to 4 speed on my main controller script (below) and crossfade to the walk animation when the left shift is held. This is my script:
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Run");
else
animation.CrossFade("Idle2");
}
I'd also like to be able to play random Idle animations at different times, but I know this is another question...
Thanks!
Answer by kru · Jan 20, 2013 at 02:53 AM
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
{
// decide between run and walk here
if (Input.GetKey(KeyCode.LeftShift) == true)
{
// we're walking
MyCharacterSpeedVariable = 4;
animation.CrossFade("Walk");
}
else
{
// we're running
MyCharacterSpeedVariable = 10;
animation.CrossFade("Run");
}
}
else
{
animation.CrossFade("Idle2");
}
Set the speed variable and values to whatever is appropriate for your project.
Hmm, when I place put this script on the character, she still moves but animations don't play at all. Yes, they are all placed in the object, so none are missing... is there somthing I'm doing wrong?
Answer by simco500 · Jan 20, 2013 at 02:52 AM
The Mecanim tutorial from Unity is perfect for this sort of things. Here it is.
It requires no scripting at all :)
Interesting, I'll have a look at this, it will surely come in useful. c:
Answer by Chronos-L · Jan 20, 2013 at 02:13 AM
I am not sure why you are going for Mathf.Abs()
, but I'm still going to use it because it is your code.
function Update() {
if( Mathf.Abs( Input.GetAxis("Vertical") ) > 0.1 ) {
//Input.GetKey() returns true while the key is being hold down
if( Input.GetKey( KeyCode.LeftShift ) ) {
animation.CrossFade("Walk");
}
else {
animation.CrossFade("Run");
}
}
else {
animation.CrossFade("Idle2");
}
}
As a bonus, I will propose a solution for your random idle animation
var idleAnimation : string[] = { "idle1", "idle2" .... };
animation.CrossFade( idleAnimation[ Random.Range(0, idleAnimation.Length) ] );
But you will have to figure out how to time the CrossFade()
of the idle animations so that you will get a smoother transition between different idle animations. You can use CrossFadeQueue(), but you will have to figure out how to code that part yourself.
This works great, thanks, my only problem is that the speed doesn't change. It could be affected by the third person script I use, which moves my character:
var speed = 6.0; var runSpeed = 9.0; var rotateSpeed = 90;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); //Deter$$anonymous$$e the player's forward speed based upon the input.
moveDirection = transform.TransformDirection(moveDirection); //make the direction relative to the player.
if(Input.GetButton("Jump")) {
moveDirection *= runSpeed;
}
else {
moveDirection *= speed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// $$anonymous$$ove the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
transform.Rotate(0, rotateSpeed * Time.deltaTime * Input.GetAxis("Horizontal"), 0);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Sorry it's so long... and thanks a million for the idle script too, I'll play around with that to see if I can get something smooth.
Your answer
Follow this Question
Related Questions
play animation when moving,and idle animation when stand still 1 Answer
Different footstep sounds problem! 2 Answers
Enemy Animation Freezing in first frame 2 Answers
why my Animation "Run" wont work 1 Answer
How do i run scripts more times 2 Answers