- Home /
There is a problem with the code, possibly the animator as well... Please help?
I think the problem may be with the animator but I couldn't be overly sure as I am a beginner. but just in case I have included the code in the space below. The problem is that my character will not switch animations at the correct time, if more screen shots are needed then please specify what parts of the program you need
Animator Screenshots:
http://www.filedropper.com/screenshot2015-03-03at92640pm_1
http://www.filedropper.com/screenshot2015-03-03at92709pm_1
http://www.filedropper.com/screenshot2015-03-03at92723pm_1
http://www.filedropper.com/screenshot2015-03-03at92730pm_1
http://www.filedropper.com/screenshot2015-03-03at92738pm_1
using UnityEngine; using System.Collections;
public class Hedgehog : MonoBehaviour {
private Rigidbody2D _myRigidBody;
private Animator _animator;
private bool _facingRight = true;
private bool _canJump;
public float speed;
public Transform startPos;
public Transform endPos;
public LayerMask groundLayer;
public float speedInc;
void Start ()
{
_myRigidBody = this.rigidbody2D;
_animator = GetComponent<Animator> ();
_canJump = true;
speedInc = 0.1f;
}
void Update ()
{
float move = Input.GetAxisRaw("Horizontal");
_animator.SetFloat ("Speed", Mathf.Abs(move));
_myRigidBody.velocity = new Vector2 (move * speed, _myRigidBody.velocity.y);
Debug.Log (speed);
speed += speedInc * Time.deltaTime;
if (speedInc >= 10)
{
speedInc = 0;
}
if(speed >= 5 && speed <= 9)
{
walk ();
}
if (speed >= 10)
{
run();
}
/*RaycastHit2D hitInfo = Physics2D.Linecast (startPos.position, endPos.position, groundLayer.value);
Debug.DrawLine (startPos.position, endPos.position);
if (hitInfo.collider != null)
{
_canJump = true;
_animator.SetBool ("Jump", false);
}
if (Input.GetKeyDown (KeyCode.Space) && _canJump == true)
{
_animator.SetBool ("Jump", true);
_myRigidBody.velocity = new Vector2 (_myRigidBody.velocity.x, 10);
_canJump = false;
}*/
}
void walk()
{
float move = Input.GetAxisRaw("Horizontal");
_animator.SetFloat ("Speed", Mathf.Abs(move));
_myRigidBody.velocity = new Vector2 (move * speed, _myRigidBody.velocity.y);
if (_facingRight == true && move < 0)
{
_facingRight = false;
transform.rotation = Quaternion.Euler (transform.rotation.x, 180, transform.rotation.z);
}
if (_facingRight == false && move > 0)
{
_facingRight = true;
transform.rotation = Quaternion.Euler (transform.rotation.x, 0, transform.rotation.z);
}
}
void run()
{
float move = Input.GetAxisRaw("Horizontal");
_animator.SetFloat ("Speed", Mathf.Abs(move));
_myRigidBody.velocity = new Vector2 (move * speed, _myRigidBody.velocity.y);
if (_facingRight == true && move <= 5)
{
_facingRight = false;
transform.rotation = Quaternion.Euler (transform.rotation.x, 180, transform.rotation.z);
}
if (_facingRight == false && move >= 9)
{
_facingRight = true;
transform.rotation = Quaternion.Euler (transform.rotation.x, 0, transform.rotation.z);
}
}
}
Answer by tebandesade · Mar 18, 2015 at 09:59 PM
Have you tried playing around with the options that are of the transition ( Has exit time, TransitionDuration etc) ? Maybe you can play around there and smoothen the animation. Or also changing the values in where each animation begins, as well as the speed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Floating boss Hands, can't move bones of hands for attacks 0 Answers
Animator.Rebind didn't work. 1 Answer
How to resume animation where it left off? 3 Answers
Need help with wall cover system(splinter cell like) 0 Answers