- Home /
i cant figure out what is wrong with my code. it is for animation transition and basic movement controls.
(public class platformercontroller : MonoBehaviour {
private Rigidbody2D rb;
public float speed;
public float jumpForce;
private bool isGrounded;
public float checkRadius;
public LayerMask whatIsGround;
private bool doubleJump;
public float jumpTime;
private bool isJumping;
private float jumpTimeCounter;
private float moveInput;
public Transform feetPos;
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("take off");
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if(isGrounded == true)
{
doubleJump = false;
anim.SetBool("isJumping", false);
}
else {
anim.SetBool("isJumping", true);
}
if(Input.GetKey(KeyCode.Space) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if(isGrounded == false && doubleJump == false && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
doubleJump = true;
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
float moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if(moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if(moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
} })
How can we know better what's wrong with your code if you don't explain what's wrong with the current behaviour and what exactly you want to achieve?
This is a 2D player Controller that was working fine until i tried to add the animation transition part. The animation transition code is supposed to change from idle to run when i tap the arrow keys and to jump from any state. the jump has both a take off animation and land animation.
Your answer
Follow this Question
Related Questions
How can I have a CharacterController's height make it to where the feet are correctly on the ground? 0 Answers
Why are my animations not working correctly? Thanks! 0 Answers
2D Character Animation - even starting if I'm not pressing A or D 2 Answers
Character will not go from Ide to Walk 1 Answer
My Animation Is Stuck On A Frame 0 Answers