Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create a simple walking script (C#)?

Discussion in '2D' started by yawnm3, Dec 20, 2014.

  1. yawnm3

    yawnm3

    Joined:
    Dec 20, 2014
    Posts:
    10
    Hi, I am a total newb in Unity and I am having problem with just the movement of my sprite..I have added a rigidbody2d to my sprite and collisionbox2d to the background. Set the collisionbox2d smaller in size for the ground only and done.

    I am using MonoDevelop and I do not know much of C#. My main programming language is C++ so thankfully there's some similarities.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Walking : MonoBehaviour {
    5.     public float WalkSpeed;
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.         if(Input.GetKey (KeyCode.LeftArrow))
    16.         {
    17.            //How do I start from here??
    18.         }
    19.  
    20.     }
    21. }
    How do I get my player position axis and set it to (x -= WalkSpeed) ? Also my MonoDevelop keep showing this error

    Sorry if I ask too much. The main problem is I do not know how my player is gonna use that movement script..The rigidbody gravity scale is set to 1 but when I click on the "Play" button, the sprite is not moving at all. Not even falling hitting the collisionbox2d.. I am really new to Unity. Hopefully you guys can provide me with simple solution and let me figure out the rest. Thanks in advance.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    sprite need colider2D too.

    put sprite in the scene, add collider2D (box or circle) to them, add rigidbody2D. Make c# script with lines:
    Code (CSharp):
    1. public float speed;
    2. private float move;
    3. void Update () {
    4. move = Input.GetAxis("Horizontal");
    5. rigidbody2D.velocity = new Vector2(move * speed, rigidbody2D.velocity.y);}
    add it to sprite gameobject.

    put ground sprite in the scene and add collider2D box (your player should go over collider and not in).

    Horizontal have default key ad,left and right, i think. You can change it in project settings - input.
     
    Last edited: Dec 20, 2014
    yawnm3 likes this.
  3. axaxi

    axaxi

    Joined:
    Jul 7, 2020
    Posts:
    5
    thanks
     
  4. Chumbinho_Codder

    Chumbinho_Codder

    Joined:
    Sep 14, 2020
    Posts:
    2
    I need a collider3D for my game. please help me
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Add Component > Select any 3D Collider?
     
  6. jofef

    jofef

    Joined:
    Sep 3, 2022
    Posts:
    1
    Hi I was wondering I have a 2d game and cant figure out how to do C# script to walk and jump can someone pls help me thanks!!!!
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Please create your own thread rather than necroing old posts. What you're asking for is covered by many, many tutorials. A forum isn't going to supply you with anything beyond what these tutorials will give you.

    "walk and jump" also isn't a single solution. Characters and games obviously have so many different dynamics but if you're doing something basic then yes, plenty of 2D tutorials out there that a quick search will give you. Follow some of them, come back if you have a specific question.