RandDGames
New member
- Joined
- Jan 6, 2021
- Messages
- 3
- Programming Experience
- Beginner
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.
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. 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.
I believe my code is exactly the same as the tutorial(s). please help.
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.
C#:
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;
}
}
I believe my code is exactly the same as the tutorial(s). please help.