using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float movespeed;
public float jumpforce;
private Rigidbody2D myRigidbody;
private Collider2D myCollider;
public bool grounded;
public LayerMask whatIsGround;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D > ();
myCollider = GetComponent<Collider2D> ();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.IsTouchingLayers (myCollider,whatIsGround);
myRigidbody.velocity = new Vector2(movespeed,myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
{
if (grounded)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpforce);
}
}
}
#Class brace is missing in your code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movespeed;
public float jumpforce;
private Rigidbody2D myRigidbody;
private Collider2D myCollider;
public bool grounded;
public LayerMask whatIsGround;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2(movespeed, myRigidbody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpforce);
}
}
}
}
check that your script has the same class as the file that you are putting on the object