Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving box help

Discussion in 'Scripting' started by PrimeLemonLime, Aug 18, 2020.

  1. PrimeLemonLime

    PrimeLemonLime

    Joined:
    Aug 16, 2020
    Posts:
    5
    Ive been trying to make a 2d box move for 12 hours now, and it just never works, even when I follow tutorials. So I was wondering if any of you have a script you can put in the comments.
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    How did it not work ? What did you try ?how you want it to move?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  4. PrimeLemonLime

    PrimeLemonLime

    Joined:
    Aug 16, 2020
    Posts:
    5
    When I put it into my character it wont give me the option to change speed or jump and wont let me move, I tried around 5 tutorials, just a simple move, jump, crouch,no sliding when moving though.
     
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Hmm..put what? Maybe you could post the script via code tag
     
  6. PrimeLemonLime

    PrimeLemonLime

    Joined:
    Aug 16, 2020
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Playermovement : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     public float jump;
    10.  
    11.     private float move;
    12.     private Rigidbody2D rb;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         move = Input.GetAxisRaw("Horizontal");
    23.  
    24.       rb.velocity = new Vector2(move * speed, rb.velocity.y);
    25.     }
    26. }
     
  7. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175

    Try this
    Everything you want is in it
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Looks good from a code standpoint.

    I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  9. PrimeLemonLime

    PrimeLemonLime

    Joined:
    Aug 16, 2020
    Posts:
    5
    thanks to both of you Ill try it