I’m starting out with Unity and I’m stuck with making a jumping script. The problem is that the ground check fails and by pressing the jump button, my character just continues to go up.
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
Vector2 myPos;
Vector2 groundCheckPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
myPos = new Vector2 (transform.position.x, transform.position.y);
groundCheckPos = new Vector2 (transform.position.x, transform.position.y);
if (Input.GetKey ("w") && !isGrounded()) {
transform.position += Vector3.up * 20 * Time.deltaTime;
}
}
public bool isGrounded(){
return Physics2D.Linecast(myPos , groundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
}
}
I have added both a layer and a tag called “Ground” to the objects Linecast should work on. What is wrong with the code?