Basic 2d Player Movement?

So I wanted to make a movement for my 2d player, I don’t want animation included, just the basic movement for my player.

And ofcourse this question is a little bit low because it is very easy, but I just need to proper settings for this!
Just A for left, D for right, and W for up, also I want to control myself in the air, and when there is a platform that moves I want to stay on top of it and not just fall of.

Also, I found some 2d movement controller scripts, but they don’t work properly.

Hopefully someone knows how to do this??

PS: for another project I want the camera only to follow the player on the x position, the rest must not move… somebody has an idea???

Thank you so much if you can help me out!

2 Likes

this isnt working properly
I just need te basics
When i hold w it can jump infinity, the gravity is really slow , so it goes really slow downwards and the block (my player) is rotating. it just need to stay.

1 Like

you can get the free Unity 2d asset with there 2d controller on the asset store.

You can also doing it yourself with some tutorial like this one i think :

3 Likes

i am new in unity 2d so please send me best tutorial link for C# script

1 Like

Hi sanjay,
First notice that this post is very old.
Second unity5 has built in movement controlled for 2d.
Third you can find script references in the documentation.
If the documentation is to hard try the unity tutorials, then try other site tutorials.
I can’t believe in took the time to reply to you when I should be reading script tutorials.
PS msdn offers free C# scripting course…

See that’s another thing ! You didn’t even state what language of script or what platform you are developing for… Try a little harder Sanjay. I spent many hours this weekend learning and I know you can do it too.

1 Like

Try here

3 Likes

thanks alot really help out

1 Like

I want my character to walk and jump Who can help?

1 Like

this is an old tread :eyes: Make player with collider2d and rigidbody2d. add new emptyobject as child to player. Place it in the bottom from player (not in the players collider). Put this empty in the rayOrigin and adjust rayCheckDistance (maybe 0.1)

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

    public float speed;
    public float jump;
    public GameObject rayOrigin;
    public float rayCheckDistance;
    Rigidbody2D rb;

    void Start () {
        rb = GetComponent <Rigidbody2D> ();
    }
  
    void FixedUpdate () {
        float x = Input.GetAxis ("Horizontal");
        if (Input.GetAxis ("Jump") > 0) {
            RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
            if (hit.collider != null) {
                rb.AddForce (Vector2.up * jump, ForceMode2D.Impulse);
            }
        }
        rb.velocity = new Vector3 (x * speed, rb.velocity.y, 0);

    }
}

check the scene for setting 5.5.2 Unity

or other way with physic2D.overlap…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{

    public LayerMask whatIsGround;
    public Transform groundCheck;
    public bool isGrounded;
    public float jumpForce;
    public float speed;
    Rigidbody2D rb;

    void Start ()
    {
        rb = GetComponent <Rigidbody2D> ();
    }

    void Update () {
        if (Input.GetButtonDown ("Jump") && isGrounded) {
            rb.AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);
            isGrounded = false;
        }
    }

    void FixedUpdate ()
    {
        isGrounded = Physics2D.OverlapPoint (groundCheck.position, whatIsGround);
        float x = Input.GetAxis ("Horizontal");
        Vector3 move = new Vector3 (x * speed, rb.velocity.y, 0f);
        rb.velocity = move;
    }
}

The layer from the ground should be changed to layer, wich you will have for ground.

2982202–221814–movePhysic2D.unitypackage (6.46 KB)

6 Likes