Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question My Code doesn't work, could someone tell me what I have done wrong?

Discussion in 'Getting Started' started by lupusgravitatis, Mar 24, 2024.

  1. lupusgravitatis

    lupusgravitatis

    Joined:
    Mar 24, 2024
    Posts:
    1
    This is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public float speed;
    private float Move;

    public float jump;

    private Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    Move = Input.GetAxis("Horizontal");

    rb.velocity = new Vector2(speed * Move, rb.velocity.y);

    if(Input.GetButtonDown("Jump"))
    {
    rb.AddForce(new Vector2(rb.velocity.x, jump));
    }
    }
    }
    However when I attach this code to my player square there is nowhere to input my speed or jump, like there is in the video tutorial I was following, could someone please tell me what I did wrong.