Animation and Stop Moving Won't Work?
Hi, I'm making a 2D infinite runner, and I am having trouble with this script.
using UnityEngine;
using System.Collections;
using System;
namespace UnityStandardAssets._2D
{
public class PlatformerCharacter2D : MonoBehaviour
{
bool CanMove = true;
bool MoveWait=false;
void OnCollissionEnter2D(Collision col1){
if (col1.gameObject.tag == "Enemy") {
GetComponent<Animation> ().Play ("RedGiantDeathToAll");
CanMove = false;
if (!MoveWait)
StartCoroutine (MoveToWait ());
}
}
public IEnumerator MoveToWait(){
MoveWait=true;
yield return new WaitForSeconds(1.5f); //Change to Animation Duration
MoveWait=false;
CanMove=true;
}
}
}
There are no errors. But it won't work. When I run the game, the animation won't play. I have 2D box colliders on all enemies and the player. I have a tag called "Enemy" and I put it on all the enemies. The tag for the enemy is the same. I have colliders on both gameObjects, and there are 2D Rigidbody's on both, too. There is no spelling errors. Turns out the death animation was named, "Die", not "RedGiantDeathToAll". It used to be named that.
Please help! I'd be grateful!!
Answer by UsmanAbbasi · May 09, 2016 at 08:05 AM
Is this condition ever becoming true? "if (col1.gameObject.tag == "Enemy") " If this line is being executed and animation is not running then check the name of this animation "GetComponent ().Play ("RedGiantDeathToAll");".
Also check the spelling of tag. There is a difference between "Enemy" and "enemy".
If the condition is not becoming true then check if function "void OnCollissionEnter2D(Collision col1)" is being called or not. If not then collisions are not being detected. To detect collisions you need to have colliders on both Game Objects and "RigidBody2d" on at least one of them. Also check that "Trigger" property in Collider inspector is not checked.
I have colliders on both gameObjects, and there are 2DRigidbody's on both, too. There is no spelling errors. Turns out the death animation was named, "Die", not "RedGiantDeathToAll". It used to be named that. Here are some errors I am having:
Ignore the first default clip. It has nothing to do with my problem. The second default clip is for the player. No animation attached for the player in the game.
Okay this is what you should do: https://www.youtube.com/watch?v=yH9_SV58SuA
Follow instructions in the video. There might be a slight difference in settings i.e. in video, "animation type" property is changed, in your unity version maybe there will be a checkbox named "Legacy", just check that checkbox and your animation will work.
It is no longer and infinite runner (the game I am making) I am holding down the right arrow to make the player move. The animation won't play, for some reason. Is it because I am holding it is no longer and infinite runner type game? I have 3 more errors:
and this:
This is the same warning you were getting before. $$anonymous$$ark the animations mentioned in warning as "Legacy" as shown in the video link.