- Home /
Question by
Lonasora · Jun 24, 2017 at 11:38 PM ·
c#2d gameanimator2d-platformeranimator controller
animation script for 2D platformer issue
Hi! im not able to make other animations to loop, i have already checked loop Time and Loop Pose boxes, i think it is somthing i did wrong with my code. For example when i want my character to walk the animation gos for 0.2 or so sec and then stops and goes to idle.
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class character_mov : MonoBehaviour
{
private CharacterController controller;
private float verticalvelocity;
private float gravity = 17.0f;
private float jumpforce = 20.0f;
private float characterspeed = 18.0f;
private Animator Char_anim;
void Start()
{
controller = GetComponent<CharacterController>();
Char_anim = GetComponent<Animator>();
}
void Update()
{
if (controller.isGrounded)
{
verticalvelocity = -gravity * 2 * (Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftAlt))
{
verticalvelocity = jumpforce;
Char_anim.SetTrigger("is_jumping");
}
}
else
{
verticalvelocity -= gravity * 3 * Time.deltaTime;
}
Vector2 moveVector = Vector2.zero;
moveVector.x = Input.GetAxis("Horizontal") * characterspeed;
moveVector.y = verticalvelocity;
controller.Move(moveVector * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
{
Char_anim.SetBool("is_walking", true);
Char_anim.SetBool("is_idle", false);
}
else
{
Char_anim.SetBool("is_walking", false);
Char_anim.SetBool("is_idle", true);
}
}
}
Comment
Best Answer
Answer by webcam · Jun 25, 2017 at 04:54 AM
GetKeyDown is only called once, right at the start of when you press a key. Every other frame your else is running setting is_walking to false. Change GetKeyDown to GetKey and it should give you a better result.
Your answer
Follow this Question
Related Questions
How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers
HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers
Multiple Cars not working 1 Answer
Animator Condition Not Working 0 Answers