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

Learning how to walk

Discussion in 'Scripting' started by blake2731, Jun 12, 2021.

  1. blake2731

    blake2731

    Joined:
    Jun 10, 2021
    Posts:
    9
    I am brand new at Unity, C#, coding in general really. and I have been watching endless tutorials but I just can't seem to get my movement script to work. I'm not sure if I just don't have keys assigned but no matter what I do it seems to just have my character sit there. I'm in 2D and my character is a sprite I made. It has a box collider and Rigidbody (although I deleted that to test the movement)

    Here's the code:

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

    public class PlayerMovement : MonoBehaviour
    {

    public float speed = 5f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");

    Vector2 pos = transform.position;

    pos.x += h * Time.deltaTime;
    pos.y += v * Time.deltaTime;

    }
    }

    Any general help would be awesome. Thanks guys!
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Please use code tags in order to better visualize your script. Thanks.

    As a starting point.. look at this.

    You are getting the position of the character. Once doing so, you are only multiplying the position obtained by your input.

    Now, knowing that... what are you doing that is applying it to the character in order to move him?

    I have never worked with 2D but I know a vector3 needs a translate method.
     
    Last edited: Jun 12, 2021
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Nothing in this code even attempts to move the object.

    What happens if you add this to the end of the Update function?
    Code (CSharp):
    1. transform.position = pos;
     
    GroZZleR and Boo-Let like this.
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    @PraetorBlue is correct and will work, but consider reading up on this to learn the difference between reference and value types in C#. You're assuming your Vector3 pos refers to the transform's position, which isn't true, it's just a copy.

    I'd strongly recommend you get out of Unity and go learn some programming fundamentals on the command line for a few weeks to give yourself a solid foundation to build from and avoid endless frustration leading to you giving up prematurely.
     
    PraetorBlue likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Welcome! Not sure which "endless tutorials" you have been watching, but generally "watching endless tutorials" is not a useful approach.

    Instead, here is how to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    For some specific GREAT tutorial guys, give these ones a try:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:

     
    GroZZleR likes this.
  6. blake2731

    blake2731

    Joined:
    Jun 10, 2021
    Posts:
    9
    Okay I'll remember that in the future thanks! The code I posted is a code from a tutorial I was watching from freecodecamp. It went in to explain how it worked but I guess I did miss something. Someone suggested a patch of code and I got some movement.
    Thanks for your input!
     
  7. blake2731

    blake2731

    Joined:
    Jun 10, 2021
    Posts:
    9
    Alright! I did that and I can move it with WASD thank you so much! Like I said I am new at this so I'm sure I got plenty of super derp mistakes I will miss in my codes. Thanks again!
     
  8. blake2731

    blake2731

    Joined:
    Jun 10, 2021
    Posts:
    9
    Thank you so much for your reply! I will definitely take your suggestions going forward to improve my skills!
    I have two doggies so I will make sure to explain the tutorials to them for now on. They'll find it very interesting I know! I actually did watch the first tutorial you shared by Imphenzia but I might have to go back and do it again and take your suggestions to do it right this time.

    Thanks again for taking the time to help me out!
     
    Kurt-Dekker likes this.
  9. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Especially if it’s about walks!
     
    blake2731 and Kurt-Dekker like this.
  10. blake2731

    blake2731

    Joined:
    Jun 10, 2021
    Posts:
    9
    Haha good point!!