Hi. I can’t get my player grounded with this script. Any advice?
using UnityEngine;
using System.Collections;
public class Movement2D : MonoBehaviour {
//Controls Player Movement
public float movementSpeed = 5.0f;
//Controls Player Jump
private int jumpHeight = 500;
public bool isGrounded = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Control PLayer Left Movement
if (Input.GetKey ("left") || Input.GetKey ("a")) {
transform.position -= Vector3.right * movementSpeed * Time.deltaTime;
}
//Control PLayer Right Movement
if (Input.GetKey ("right") || Input.GetKey ("d")) {
transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
if ((Input.GetButtonDown ("Jump") || Input.GetKey ("w")) && isGrounded) {
Jump ();
Debug.Log ("hoppar");
}
}
void Jump () {
if (!isGrounded) {
return;
}
isGrounded = false;
rigidbody2D.AddForce (new Vector2 (0, jumpHeight));
}
void FixedUpdate(){
isGrounded = Physics.Raycast (transform.position, -Vector3.up, .5f);
}
}