Question by
RandDGames · Jan 06, 2021 at 11:10 AM ·
c#movementvector2
Vector2 not being recognised?
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;
}
}
Comment