Hi.
I’m converting a javascript for a 2D platformer I wrote some time ago into C#. I’m done now, but for some reason, the animation won’t play…
in the start function I use the InvokeRepeating and call a function called “CallAnimations”
It checks if the boolean “Running” is true, it runs the “AnimateRun” function and if he isn’t running it’ll call the “AnimateJump” function.
I have the javascript attached to a player, but it’s long and include buttons and all so I didn’t post it. but it works on a prefab I have. when I take the script and attach to an empty player, he runs, but he doesn’t get animated(Yes, I have attached the textures)
and the C# script only gets the player to run and jump, but nothing else…
Here’s my code:
public Texture2D[] runFrames;
public Texture2D[] jumpFrames;
public int FPS;
private float secondsToWait;
private int currentFrame;
private bool running = true;
private bool jumping = false;
private Vector3 moveDir;
private float vSpeed;
private bool jump = false;
public float speed = 2.0f;
public float jumpSpeed = 12.0f;
public float gravity = 30.0f;
void Start ()
{
//Calls the function that calls the different animations depending
//on if the player is grounded or in the air.
InvokeRepeating("CallAnimations", 0, secondsToWait);
Debug.Log("CallAnimations");
currentFrame = 0;
//Sets the time between frames to (1 / FPS)
secondsToWait = 1 / FPS;
}
// Update is called once per frame
void Update ()
{
CharacterController controller = GetComponent<CharacterController>();
moveDir = Vector3.right * speed; //make a variable called moveDir and calculate the horizontal speed.
if (controller.isGrounded)//If the player is grounded set the vertical speed to zero
{
running = true;
vSpeed = 0.0f;
if(jump)
{
vSpeed = jumpSpeed;
}
}
else
{
Debug.Log("not grounded");
running = false;
}
if(Input.GetKey(KeyCode.Space))
{
jump = true;
}
else
{
jump = false;
}
vSpeed -= gravity*Time.deltaTime;//Apply a physically correct gravity
moveDir.y = vSpeed;//add the vertical speed
controller.Move(moveDir*Time.deltaTime);//and finally move the character
}
//This will call the different animations, if they're supposed to be playing
void CallAnimations ()
{
Debug.Log("Calling animations");
if(running == true)
{
AnimateRun();
Debug.Log("Running");
}
else
{
AnimateJump();
Debug.Log("Jumping");
}
}
//This will animate the Run Animation
void AnimateRun ()
{
//If the current frame is larger than the amount of frames
//in the animation, then it sets the current frame to 0 (restart the animation)
Debug.Log("ANIMATING RUN");
if(currentFrame >= runFrames.Length)
{
currentFrame = 0;
}
renderer.material.mainTexture = runFrames[currentFrame];
currentFrame++;
}
//This will animate the Jump Animation
void AnimateJump ()
{
//If the current frame is larger than the amount of frames
//in the animation, then it sets the current frame to 0 (restart the animation)
Debug.Log("ANIMATING JUMP");
if(currentFrame >= jumpFrames.Length)
{
currentFrame = 0;
}
renderer.material.mainTexture = jumpFrames[currentFrame];
currentFrame++;
}