Search Unity

Need Help

Discussion in '2D' started by ProgriNilsi, Apr 9, 2021.

  1. ProgriNilsi

    ProgriNilsi

    Joined:
    Apr 9, 2021
    Posts:
    2
    Hi, i need help at my Move and Jump code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Movement : MonoBehaviour
    {
    public float movementSpeed;
    public Rigidbody2D rb;

    public float jumpForce = 20f;
    public Transform feet;
    public LayerMask groundLayers;

    float mx;

    private void Update()
    {
    mx = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump") && IsGrounded()){
    Jump();

    }

    }


    private void FixedUpdate()
    {
    Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);

    rb.velocity = movement;
    }

    void Jump()
    {

    Vector2 movement = new Vector2(rb.velocity.x, jumpForce);

    rb.velocity = movement;

    }
    public bool IsGrounded(){

    Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
    if (groundCheck != null) {
    return true;
    }

    return false;
    }
    }
    if i start i cant jump. And if I add groundCheck.gameObject there is a message coming which says that the gameobject has no instance
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Can you edit your post and use code tags please? Difficult to read
     
    Deleted User likes this.