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

Help with player movement

Discussion in '2D' started by RandDGames, Jan 6, 2021.

  1. RandDGames

    RandDGames

    Joined:
    Dec 2, 2020
    Posts:
    3
    Hi all,

    Major beginner so I apologise if this seems painfully obvious.

    I'm following a tutorial to get a player sprite to move around, they have used a Vector2 with no issues, however when I typed it in visual studio doesn't recognise it. I have followed the tutorial all the way and have this code. The only thing I have found is maybe I am not using the correct namespace but I appear to be using the same ones as the tutorial, so I'm all confused now.

    Any and all help is appreciated.

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

    public class PlayerController : MonoBehaviour
    {
    public float moveSpeed;

    public bool isMoving;
    private Vector2 input;

    private void Update()
    {
    if (!isMoving)
    {
    input.x = input.GetAxisRaw("Horizontal");
    input.y = input.GetAxisRaw("Virtical");

    if (input != Vector2.zero)
    {
    var targetPos = transform.position;
    targetPos.x += input.x;
    targetPos.y += input.y;

    StartCoroutine(Move(targetPos));
    }
    }
    }
    IEnumerator Move(Vector3 targetPos)
    {
    isMoving = true;

    while ((targetPos - transform.position).sqrMagnitude > mathf.Epsilon)
    {
    transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
    yield return null;
    }
    transform.position = targetPos;

    isMoving = false;
    }
    }
     
  2. RandDGames

    RandDGames

    Joined:
    Dec 2, 2020
    Posts:
    3
    I thought I would mention the reason i think its the Vector2 is because every time Vector is used it appears white and the tutorial it appears in colour, same goes for most of the code in the coroutine
     
  3. RandDGames

    RandDGames

    Joined:
    Dec 2, 2020
    Posts:
    3
    Does anyone have any ideas cause i started again from scratch same issue, i even tried a different tutorial and same issue i read somewhere about a, using System.Collections.Numeric; still not working. please help :(
     
  4. ForsakenBacon

    ForsakenBacon

    Joined:
    Dec 1, 2020
    Posts:
    1
    Hey,

    Only starting out myself and have been getting right into 2d player movement tutorials and the different methods to move them around.

    From your code, (haven't tested myself) it looks like your vector2 variable is named input, and you're trying to set X and y to input.getaxisraw...

    So it might be getting confused between the two "input" variables. Trying changing the vector2 variable to something else.

    As for tutorials, I was working through this for a while but I stopped as it's very buggy and couldn't be expanded upon well. This uses rigidbody and adding force to move the character. But it still gives a really good knowledge on how to move a player.


    Going with something called Raycasts is a better solution for moving your player vs rigidbody (again only a beginner, but just talking from what I read). This tutorial series is great, but there's also a heck load of math he goes over, not that you need to really understand any of it to use his scripts. Raycasts are really helpful for tons of things so might be worth learning that early.
    https://youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz

    Hopefully this helps, and more expert people can add to it! Best of luck