Hello everyone. I’m doing a platform game in 2d (camera on the side of the character). I made motion animations and I have a problem with how to turn the character (animations) in both directions after pressing the “A” key.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ruch : MonoBehaviour
{
public float speed;
public float jump;
public Rigidbody2D bohater;
public float stanPrzed;
public float roznica;
private Animator animacja;
public Transform czujnik;
public float promien;
public LayerMask warstwa;
public bool dotyk;
void FixedUpdate()
{
stanPrzed = bohater.position.x;
dotyk = Physics2D.OverlapCircle (czujnik.position, promien, warstwa);
}
void Start()
{
bohater = GetComponent<Rigidbody2D> ();
animacja = GetComponent<Animator> ();
}
void Update()
{
roznica = Mathf.Abs(stanPrzed - bohater.position.x);
animacja.SetFloat("kontrola", roznica);
if (Input.GetKey (KeyCode.D)){
bohater.velocity = new Vector2 (speed, bohater.velocity.y);
}
if (Input.GetKey (KeyCode.A)){
bohater.velocity = new Vector2 (-speed, bohater.velocity.y);
}
if (Input.GetKey (KeyCode.Space) && dotyk){
bohater.velocity = new Vector2 (bohater.velocity.x, jump);
}
}
}