- Home /
Simple animation not playing using animation.Play();
I have two of the following, one for the left and one for the right. This moves the players position. There is a problem with the animation not playing though. Ive used javascript for it in the past and assign
var Animator : GameObject;
Which i dont think you do for c#?
Any help is appreciated, thanks.
using System.Collections;
public class right_Movement : MonoBehaviour {
public Texture2D button1; // Inital texture for the button
public Texture2D button2; // Once pressed texture
public GameObject Player; // Declares the player from a gameobject
public int speed;
public Animator anim;
void Start ()
{
anim = gameObject.GetComponent<Animator>();
guiTexture.texture = button1; // gui texture is made into button1
}
void Update () // If the screen is touched
{
foreach (Touch touch in Input.touches)
{
if (guiTexture.HitTest(touch.position) && touch.phase != TouchPhase.Ended) // If the gui texture is touched then
{
guiTexture.texture = button2; // The button changes to button2
anim.SetBool ("run_right", true);
Player.transform.Translate(Vector3.right * speed * Time.smoothDeltaTime); // Player slowly moves to the right
}
else if (guiTexture.HitTest(touch.position) && touch.phase == TouchPhase.Ended) // until the player stops touching the screen
{
guiTexture.texture = button1; // gui texture reverts back to the original
anim.SetBool ("run_right", true);
}
}
}
}
is that animation "left_run" added through code, or through the editor?
Left run has not been added at all. All of them - bar the idle - are in the project window waiting advise. I dont know how to assign "left_run".
Then add it =P
Add the "Animtion" component and put the clip in the Animation slot.
Could you package up the script and animation and post it here? I can take a look at it. I don't think I can debug it with just the info you posted. $$anonymous$$aybe someone else can, but I need to see settings and such :{ I know what it feels like spending hours on a glitch. Its terrible =(
The thing is you have to run unity remote to check if its working. I could send you the project, however i will remove art e.t.c as this has been a project for sometime now, then i upgrade to 4.3 for the new 2d section and this happens ahha i don't feel comfortable sharing the project to everyone though.
Answer by corriedotdev · Mar 22, 2014 at 01:46 PM
Turns out that the gameobject was assigned to the player but i called gameobject instead of player. Silly
Answer by suribe · Mar 21, 2014 at 07:48 AM
In the Animation component, set the Animations value (collapsed in the picture you show) to 1. It will then show a slot for adding an animation, similar to the one called "Animation" which now says "None". Drag from your project resources the "left_run" animation to the slot, and try again.
Thanks for helping me out, i have spent the past 10 $$anonymous$$utes looking at what you said and not sure what you mean. Did you want me to set the animations to legacy? I would appreciate any help man really, thanks!
Is there a console message complaining about legacy animations?
There are some rough parts in the whole Animator vs. Animation handling, and this "marked as Legacy" is really annoying, because there is actually no way to mark an animation as legacy. :) You have both an Animator and Animation component. If you are doing this from scratch, I suggest just leave the animator, read the tutorials on how to use it, and create different states for you animation, switching between them with a trigger. It's more work at first, but once you have it setup it will probably be easier to add new animations. And it is clearly the way Unity will be in the future, everything inside Animation Controllers.
Setting it to legacy is in the animations debug windows with a value of 1 ins$$anonymous$$d of the default of 2? No? I understand what your saying.
So basically there is nothing obvious that is wrong with my script and set up so i have to look into the animator. Damn haha
Ok so i have managed to get the animation to work on the pc with no problems. I made another question here which has both scripts. Basically there is something wrong with my android code for playing it and i dont know what it is. Thanks
Your answer
Follow this Question
Related Questions
Converted PC player controls to Mobile not working 2 Answers
Multiple Cars not working 1 Answer
Help with c# code - Multitouch 1 Answer
Turning touch to rotation 1 Answer