Search Unity

Basic 2d Player Movement?

Discussion in '2D' started by alexanderh1005, Jul 19, 2014.

Thread Status:
Not open for further replies.
  1. alexanderh1005

    alexanderh1005

    Joined:
    Jul 18, 2014
    Posts:
    4
    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!
     
    lalit_dev and russ147281 like this.
  2. alexanderh1005

    alexanderh1005

    Joined:
    Jul 18, 2014
    Posts:
    4
    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.
     
    rayan_farsat likes this.
  3. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    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 :
     
  4. sanjay2145

    sanjay2145

    Joined:
    Jul 3, 2015
    Posts:
    1
    i am new in unity 2d so please send me best tutorial link for C# script
     
    russ147281 likes this.
  5. Skeleetor

    Skeleetor

    Joined:
    Jul 18, 2015
    Posts:
    3
    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.
     
    polarizadosmedellin likes this.
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  7. SmashteamGX

    SmashteamGX

    Joined:
    Jul 21, 2016
    Posts:
    3
     
  8. SmashteamGX

    SmashteamGX

    Joined:
    Jul 21, 2016
    Posts:
    3
    tedthebug likes this.
  9. ultrabeastkaveh

    ultrabeastkaveh

    Joined:
    Mar 5, 2017
    Posts:
    1
    I want my character to walk and jump Who can help?
     
    saurav19271 likes this.
  10. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this is an old tread o_O 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)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Move : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public float jump;
    8.     public GameObject rayOrigin;
    9.     public float rayCheckDistance;
    10.     Rigidbody2D rb;
    11.  
    12.     void Start () {
    13.         rb = GetComponent <Rigidbody2D> ();
    14.     }
    15.  
    16.     void FixedUpdate () {
    17.         float x = Input.GetAxis ("Horizontal");
    18.         if (Input.GetAxis ("Jump") > 0) {
    19.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
    20.             if (hit.collider != null) {
    21.                 rb.AddForce (Vector2.up * jump, ForceMode2D.Impulse);
    22.             }
    23.         }
    24.         rb.velocity = new Vector3 (x * speed, rb.velocity.y, 0);
    25.  
    26.     }
    27. }
    check the scene for setting 5.5.2 Unity

    or other way with physic2D.overlap...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour
    6. {
    7.  
    8.     public LayerMask whatIsGround;
    9.     public Transform groundCheck;
    10.     public bool isGrounded;
    11.     public float jumpForce;
    12.     public float speed;
    13.     Rigidbody2D rb;
    14.  
    15.     void Start ()
    16.     {
    17.         rb = GetComponent <Rigidbody2D> ();
    18.     }
    19.  
    20.     void Update () {
    21.         if (Input.GetButtonDown ("Jump") && isGrounded) {
    22.             rb.AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);
    23.             isGrounded = false;
    24.         }
    25.     }
    26.  
    27.     void FixedUpdate ()
    28.     {
    29.         isGrounded = Physics2D.OverlapPoint (groundCheck.position, whatIsGround);
    30.         float x = Input.GetAxis ("Horizontal");
    31.         Vector3 move = new Vector3 (x * speed, rb.velocity.y, 0f);
    32.         rb.velocity = move;
    33.     }
    34. }
    35.  
    The layer from the ground should be changed to layer, wich you will have for ground.
     

    Attached Files:

    Last edited: Mar 17, 2017
Thread Status:
Not open for further replies.