- Home /
Multiple Animations for the Same GameObject
I am writing my own, more advanced FPS controller and I have come across a problem with animating. I try to create multiple animations in my FPS Controller GameObject but it says, "Can't add the same object multiple times!". This is really frustrating me because I want to have both a running and a walking animation. Heres my FPS code so far, var rigidBody : Rigidbody; var fpsController : Transform; var fpsGameObject : GameObject;
var fpsCamera : Transform;
var bobHeadRunningAnimation : Animation;
var bobHeadWalkingAnimation : Animation;
var jumpVelocity : float;
var forwardWalkVel : float;
var sideWalkVel : float;
var backWalkVel : float;
var runVel : float;
private var isCrouching : boolean = false;
private var beforeCrouchHeight : float = 0;
function Update () {
if (Input.GetKeyDown(KeyCode.Space))
prepareJump();
if (Input.GetKeyUp(KeyCode.Space))
jump();
if (Input.GetKey(KeyCode.W)) {
fpsController.Translate(Vector3.forward * forwardWalkVel * Time.deltaTime);
bobHead(false);
}
if (Input.GetKey(KeyCode.A)) {
fpsController.Translate(Vector3.left * sideWalkVel * Time.deltaTime);
bobHead(false);
}
if (Input.GetKey(KeyCode.S)) {
fpsController.Translate(Vector3.back * backWalkVel * Time.deltaTime);
bobHead(false);
}
if (Input.GetKey(KeyCode.D)) {
fpsController.Translate(Vector3.right * sideWalkVel * Time.deltaTime);
bobHead(false);
}
if (Input.GetKeyDown(KeyCode.C)) {
if (isCrouching == false) {
beforeCrouchHeight = fpsController.position.y; //Save Before Crouch Height
fpsGameObject.transform.localScale = new Vector3(1, 0.52, 1); //0.5 wouldn't work!
isCrouching = true;
}
else if (isCrouching == true) {
fpsGameObject.transform.localScale = new Vector3(1, 1, 1);
fpsController.position.y = beforeCrouchHeight; //Reset Height
beforeCrouchHeight = 0; //Reset Value
isCrouching = false;
}
}
if (Input.GetKey(KeyCode.LeftShift)) {
fpsController.Translate(Vector3.forward * runVel * Time.deltaTime);
bobHead(true);
if (Input.GetKeyUp(KeyCode.LeftShift))
bobHeadRunningAnimation.Stop();
}
}
function prepareJump() {
fpsCamera.Translate(Vector3.down * 0.1);
forwardWalkVel /= 2;
sideWalkVel /= 2;
backWalkVel /= 2;
}
function jump() {
fpsCamera.Translate(Vector3.up * 0.1);
rigidBody.AddRelativeForce(0, jumpVelocity, 0);
forwardWalkVel *= 2;
sideWalkVel *= 2;
backWalkVel *= 2;
}
function bobHead(isRunning : boolean) {
if (isRunning == true)
bobHeadRunningAnimation.Play();
else
//Cannot do anything until I have another animation
}
Here is the screen shot of my inspector:
Here is a screenshot of the error:
Answer by ASPePeX · Jan 30, 2017 at 08:33 AM
The exact error message is important, it says component not object! You can indeed only add one Animation component but it can contain multiple animations.
The first image should explain everything: https://docs.unity3d.com/Manual/class-Animation.html
Edit: well, I didn't see this question was necroed from 2014 ...
Your answer
Follow this Question
Related Questions
Problem with script. (Sorry for huge code) 1 Answer
String.Split() Function Issues 1 Answer
Talking Code 2 Answers
In Game Animation 2 Answers
character lagging in air when jumped 0 Answers