My movement gets blocked, when I activate the animator
Hello guys, Iam pretty new to Unity and try to get the hang on by watching a lot of tutorials. At the moment Iam working on a 2D platformer. I use the script from "GamesfromJames" on Youtube. But now I stuck at one point.
Without any animations my character moves on the x and jumps on the y axis. When I activate the animator my character stucks in the air playing the idle-animation. When I activate the Apply Root Motion box he falls onto the ground and plays the idle animation. At no point at all I can move my character, when the animator is activated.
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
public bool grounded;
public LayerMask whatIsGround;
private Collider2D myCollider;
private Rigidbody2D myRigidBody;
private Animator myAnimator;
// Use this for initialization
void Start() {
myRigidBody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update() {
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
if (Input.GetKeyDown(KeyCode.Space)) ;
{
if (grounded)
{
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);
}
}
if (Input.GetKey(KeyCode.D))
{
myRigidBody.velocity = new Vector2(moveSpeed, myRigidBody.velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
myRigidBody.velocity = new Vector2(-moveSpeed, myRigidBody.velocity.y);
}
myAnimator.SetFloat("Speed", myRigidBody.velocity.y);
myAnimator.SetBool("Grounded", grounded);
}
}
The only differences I know I did from the youtube video "Unity Endless Runner Tutorial #3 - Animations & Following Camera" is the object sprite. I animated my game object inside of unity using a lot of layers (for arms, legs and so on).
Could you help me? Big thanks in advance!
Answer by Vestire · Jan 09, 2017 at 07:29 PM
Ive found the reason for my problem. For everyone in the near future with the same issues: The problem is, that the animator collides with the script in the same place of the hierachy. Just place the animator as a child of boxscript/rigidbody and player controller and everthing's fine.
Your answer
Follow this Question
Related Questions
Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers
Trying to change an animation with a UI button 2 Answers
Animator transition not working on parameter 0 Answers
2D Animation disables Player Controller Script 1 Answer
SetBool animation state based on direction object is moving 0 Answers