Poco más que añadir, cuando en unity le doy play a la escena automaticamente el personaje se sale de plano moviendose solo en el eje X. Aqui os dejo el código. Gracias de antemano
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_Script : MonoBehaviour
{
Animator an;
SpriteRenderer mySpriteRenderer;
Rigidbody2D rb;
float movementDir;
float speed;
bool canJump;
void Start()
{
movementDir = 1.0f;
speed = 250.0f;
an = gameObject.GetComponent<Animator>();
rb = gameObject.GetComponent<Rigidbody2D>();
}
private void Awake()
{
mySpriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
Vector2 currentVel = rb.velocity;
//*****************JUMP***************************
if (Input.GetKeyDown(KeyCode.Space) && canJump)
{
canJump = false;
an.SetInteger("state", 2);
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 300f));
}
//**************MOVE RIGHT*************************
if (Input.GetKey(KeyCode.RightArrow))
{
an.SetInteger("state", 1);
mySpriteRenderer.flipX = false;
currentVel.x = 1.0f;
if (Input.GetKey(KeyCode.LeftShift))
{
an.SetInteger("state", 3);
currentVel.x = 2.0f;
}
}
//***************MOVE LEFT**************************
else if (Input.GetKey(KeyCode.LeftArrow))
{
mySpriteRenderer.flipX = true;
an.SetInteger("state", 1);
currentVel.x = -1.0f;
if (Input.GetKey(KeyCode.LeftShift))
{
an.SetInteger("state", 3);
currentVel.x = -2.0f;
}
}
else
{
an.SetInteger("state", 0);
currentVel.x = 0;
}
movementDir = Input.GetAxis("Horizontal");
rb.AddForce(new Vector3(movementDir, 0.0f, 0.0f) * speed * Time.deltaTime);
rb.velocity = currentVel;
}
private void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "ground")
{
canJump = true;
}
}