Advertisement

Issues with sprite/animator/animation on my Unity project

Started by June 02, 2021 03:17 PM
0 comments, last by MuteMist 3 years, 5 months ago

Hi fellow game designers, currently i´m not working on a established proyect as a whole (yet) but getting to know Unity 2d engine.

I´ve been doing minor adjustments and working on the most basic aspects of a character. Movement, basic movement and making the sprite change while executing those actions.

Ok with the ground settled, the animations on the animator have already been established and the parameters with them.

I did a basic script regarding horizontal movement and jump.

The next step was adding an animator function to that script, i tried replicating a Brackeys tutorial regarding the animation on Unity.

However for some reason, adding that animator function made null the movement functions.

Any suggestions? (I´ll leave the code) and i´m totally open to remaking the script or looking at tutorials/documentation.

public class Character2DController : MonoBehaviour
{
    public float _speed = 2f;
    public float JumpForce = 1f;

    private Rigidbody2D _rigidbody;

    void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {

        
        var _move = Input.GetAxis("Horizontal");
        transform.position = transform.position + new Vector3(_move * _speed * Time.deltaTime, 0, 0);

        if (!Mathf.Approximately(0, _move))
            transform.rotation = _move > 0 ? Quaternion.Euler(0, 180, 0) : Quaternion.identity; 

        if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
        {
            _rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
        }
    }
}

Thank you in advice!

This topic is closed to new replies.

Advertisement