Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't move my character with Animator on

Discussion in '2D' started by janjicm, May 1, 2020.

  1. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    Here is the video with animator off, animator on without Root Motion and animator on with Root Motion.


    Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Kontrola : MonoBehaviour
    {
    [SerializeField] private LayerMask platformsLayerMask;
    [SerializeField] private Transform pfDoubleJumpEffect;
    [SerializeField] private Transform pfAirJumpIcon;

    public float moveSpeed = 10f;
    public float jumpVelocity = 10f;

    private Rigidbody2D rigidbody2d;
    private BoxCollider2D boxCollider2d;
    private int airJumpCount;
    private int airJumpCountMax;
    private List<GameObject> airJumpIconGameObjectList;
    private float moveInput;

    [Header("Wall Jump")]
    public float wallJumpTime = 0.2f;
    public float wallSlideSPeed = 0.3f;
    public float wallDistance = 0.5f;
    bool isWallSliding = false;
    RaycastHit2D WallCheckHit;
    float jumpTime;

    private Animator anim;

    bool isFacingRight = true;

    private void Awake()
    {
    rigidbody2d = transform.GetComponent<Rigidbody2D>();
    boxCollider2d = transform.GetComponent<BoxCollider2D>();
    SetAirJumpCountMax(1);
    anim = GetComponent<Animator>();
    }

    private void Update(){
    if (IsGrounded())
    {
    ResetAirJumpCount();
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
    if(IsGrounded())
    {
    float jumpVelocity = 12f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    }
    else if(isWallSliding && Input.GetKey(KeyCode.UpArrow))
    {
    float jumpVelocity = 7f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    }
    else
    {
    if(Input.GetKeyDown(KeyCode.UpArrow ))
    {
    if(airJumpCount < airJumpCountMax)
    {
    Instantiate(pfDoubleJumpEffect, transform.position, Quaternion.identity);
    float jumpVelocity = 10f;
    rigidbody2d.velocity = Vector2.up * jumpVelocity;
    SpendAirJump();
    }
    }
    }
    }

    HandleMovement();
    }

    private void SetAirJumpCountMax(int airJumpCountMax)
    {
    this.airJumpCountMax = airJumpCountMax;
    airJumpIconGameObjectList = new List<GameObject>();
    for (int i = 0; i < airJumpCountMax; i++)
    {
    Transform airJumpIconTransform = Instantiate(pfAirJumpIcon, transform);
    airJumpIconTransform.localPosition = new Vector3(-0.15f, 1f * i, 0.0009f);
    airJumpIconGameObjectList.Add(airJumpIconTransform.gameObject);
    }
    }

    private void SpendAirJump(){
    airJumpCount++;
    for (int i = 0; i < airJumpCount; i++)
    {
    airJumpIconGameObjectList.SetActive(false);
    }
    }

    private void ResetAirJumpCount()
    {
    if(airJumpCount > 0){
    airJumpCount = 0;
    for (int i = 0; i < airJumpIconGameObjectList.Count; i++)
    {
    airJumpIconGameObjectList.SetActive(true);
    }
    }
    }



    private bool IsGrounded()
    {
    RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .4f, platformsLayerMask);
    return raycastHit2d.collider != null;
    }

    void HandleMovement()
    {
    moveInput = Input.GetAxisRaw("Horizontal");
    rigidbody2d.velocity = new Vector2(moveInput * moveSpeed, rigidbody2d.velocity.y);

    if(isFacingRight == false && moveInput > 0)
    {
    Flip();
    }
    else if(isFacingRight == true && moveInput < 0)
    {
    Flip();
    }

    if (moveInput == 0)
    {
    anim.SetBool("isRunning", false);
    }
    else
    {
    anim.SetBool("isRunning", true);
    }

    }

    void FixedUpdate(){

    if (isFacingRight)
    {
    WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(wallDistance, 0), wallDistance, platformsLayerMask);
    }
    else
    {
    WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(-wallDistance, 0), wallDistance, platformsLayerMask);
    }

    if (WallCheckHit && moveSpeed != 0)
    {
    isWallSliding = true;
    jumpTime = Time.time + wallJumpTime;
    }
    else if (jumpTime < Time.time)
    {
    isWallSliding = false;
    }

    if (isWallSliding)
    {
    rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, Mathf.Clamp(rigidbody2d.velocity.y, wallSlideSPeed, float.MaxValue));
    }
    }

    void Flip ()
    {
    isFacingRight = !isFacingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
    }


    }
     
  2. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    I fixed it! I had one frame set to change players position instead of his sprite.