Hi, my problem is the following: my character can’t jump, although I tried to do my best.
I learn 2D creation on Unity web-site, but I’m totally stucked now on this problem.
The most astonishing moment for me is that I do everything correct. This refers to scripting and steps.
One moment I thought that problem is with my version of Unity, because I was using Unity 5, but tutorial is with Unity 4.5.
But even this didn’t help, moreover all previous actions which worked on Unity 5 began not work on Unity 4.5
EDIT:
public float maxSpeed=10f;
bool facingRight=true;
Animator anim;
bool grounded=false;
public Transform groundCheck;
float groundRadius=0.2f;
public LayerMask whatIsGround;
public float jumpForce=700f;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update ()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool ("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpForce));
}
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}