- Home /
Character Animation Question (Bear Force One)
We created a script for our main character (Vlad the Bear), and it links keyboard input to animations we have made in Blender. It goes as follows:
var speed = 3.0; var rotateSpeed = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetAxis("Vertical") )
{
animation.CrossFade("twolegrun");
}
if(curSpeed < 0.0001)
{
animation.CrossFade("stillup");
}
if(Input.GetAxis("Vertical") && Input.GetAxis("Horizontal"))
{
animation.CrossFade("twolegrun");
}
if(Input.GetAxis("Jump") && Input.GetAxis("Vertical"))
{
animation.CrossFade("twolegattack");
}
if(Input.GetAxis("Jump"))
{
animation.CrossFade("twolegattack");
}
}
@script RequireComponent(CharacterController)
Okay, so when we play it, the animations play for the walking around. However the animation for attacking only plays when the spacebar is held. Is there any way to go about the scripting for the attack as to play the animation fully when the spacebar is tapped, and to return to the moving scripting/animations?
Also is there an absolute value code? I would like to make the bear be able to move forward and turn sideways, but not to be able to walk backwards as it looks unnatural with our current aniamtions.
Thank you !
Answer by zinnfandel · Apr 01, 2011 at 10:45 PM
First, for the jump you can use
Input.GetButtonDown("Space")
This fires only once when the key is pressed, then will be checked only after the key has been released and pressed again, so it's good for one-time actions.
For the absolute value you can use:
(Mathf.Abs(YourVariable))
Please seperate your questions next time though. Hope it works :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
mystery RestartLevel function used in official tutorial 2 Answers
No movement when animation plays (2d) 1 Answer
how to make your own character. 2 Answers
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers