Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Can't get character movement to work

Discussion in '2D' started by Blyzzums, Feb 22, 2021.

  1. Blyzzums

    Blyzzums

    Joined:
    Feb 21, 2021
    Posts:
    1
    As I'm sure you can guess, I am brand new to Unity and coding as a whole.. Like genuinely zero clue what I am doing. I got bored and decided to start making some sprites and stuff and figured why not make a little 2d game. All was well until I got to making the character move, as I just can't get it to work.
    I have looked up many tutorials and they're either too basic, or outdated. I found one which has worked ALMOST completely:

    After following the video and copying the code up until the 6:12 mark, I had no errors and unity seemed to think everything was fine, yet when I went to put the child PlayerPos thingy into the Player Pos variable in the unity script (menu? idk) I don't have the option for it. He has a youtube comment from only 4 days ago saying that it worked well, yet it doesn't for me.
    The jumping also doesn't seem to work. Yet, the character still moves.
    I watched the rest of the video and put the code in that he did after this in order to add character flipping and remove the floating, and now I am getting an error "Object reference not set to an instance of an object" which I am assuming is referring to the lack of a PlayerPos object which he has.

    I did this before watching a few videos on the basics, so instead of knowing 0% it's probably now more around 0.1% and I still can't find the issue
    Sorry if this is a bit long, first time posting idk whats what

    EDIT: The only thing I have noticed is that when he was typing out "private Transform playerPos" his "Transform" automatically turned green as if it was a class, which mine didn't. The same thing happened with "LayerMask" I have zero clue how to fix that.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody2D rb;
    9.     private SpriteRenderer sr;
    10.     public float moveSpeed;
    11.     public float jumpSpeed;
    12.     public float moveInput;
    13.  
    14.     private bool isOnGround;
    15.     private Transform playerPos;
    16.     public float positionRadius;
    17.     public LayerMask ground;
    18.  
    19.     private float airTimeCount;
    20.     public float airTime;
    21.     private bool inAir;
    22.  
    23.     void Start()
    24.     {
    25.         rb = GetComponent<Rigidbody2D>();
    26.         sr = GetComponent<SpriteRenderer>();
    27.     }
    28.  
    29.     void FixedUpdate()
    30.     {
    31.         moveInput = Input.GetAxisRaw("Horizontal");
    32.         rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground);
    38.  
    39.         if(moveInput > 0)
    40.         {
    41.             sr.flipX = false;
    42.         } else if(moveInput < 0)
    43.         {
    44.             sr.flipX = true;
    45.         }
    46.  
    47.  
    48.         if(isOnGround == true && Input.GetKeyDown(KeyCode.Space))
    49.         {
    50.             inAir = true;
    51.             airTimeCount = airTime;
    52.             rb.velocity = Vector2.up * jumpSpeed;
    53.         }
    54.  
    55.         if(Input.GetKey(KeyCode.Space) && inAir == true)
    56.         {
    57.             if(airTimeCount > 0)
    58.             {
    59.                 rb.velocity = Vector2.up * jumpSpeed;
    60.                 airTimeCount -= Time.deltaTime;
    61.             }
    62.             else
    63.             {
    64.                 inAir = false;
    65.             }
    66.         }
    67.  
    68.         if(Input.GetKeyUp(KeyCode.Space))
    69.         {
    70.             inAir = false;
    71.         }
    72.     }
    73.  
    74.  
    75. }
    76.  
     
unityunity