Resolved Vector2 is not recognised

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.
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;
}
}
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
Do you have an error on line 3 regarding the UnityEngine? I'm wondering if you didn't correctly add a reference to UnityEngine assembly.
 

RandDGames

New member
Joined
Jan 6, 2021
Messages
3
Programming Experience
Beginner
Do you have an error on line 3 regarding the UnityEngine? I'm wondering if you didn't correctly add a reference to UnityEngine assembly.
hi thank you for replying, i have these errors:

Assets\Scripts\Player\PlayerController.cs(16,29): error CS1061: 'Vector2' does not contain a definition for 'GetAxisRaw' and no accessible extension method 'GetAxisRaw' accepting a first argument of type 'Vector2' could be found (are you missing a using directive or an assembly reference?) X2

Assets\Scripts\Player\PlayerController.cs(33,64): error CS0103: The name 'mathf' does not exist in the current context X1

The unityengine reference was already in the code when i created it.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
There's multiple problems at play here, but it comes down to you want to be using Input.GetAxisRaw("Horizontal"), not input.GetAxisRaw("Horizontal"). Note the capital 'I'.

The input (with a lowercase 'i') is actually a Vector2 struct which you declared on line 10. You are trying to use it like the Input class which will obviously not work.

Regarding the mathf error: It should be Mathf.

In C#, case matters because it is a case sensitive language.
 

RandDGames

New member
Joined
Jan 6, 2021
Messages
3
Programming Experience
Beginner
There's multiple problems at play here, but it comes down to you want to be using Input.GetAxisRaw("Horizontal"), not input.GetAxisRaw("Horizontal"). Note the capital 'I'.

The input (with a lowercase 'i') is actually a Vector2 struct which you declared on line 10. You are trying to use it like the Input class which will obviously not work.

Regarding the mathf error: It should be Mathf.

In C#, case matters because it is a case sensitive language.
Thanks so much, this now compiles but the code doesn't seem to do anything lol, i'm gonna try to work it out again. but thank you for helping.
 

Pharma

New member
Joined
Jan 9, 2022
Messages
2
Programming Experience
Beginner
I'm assuming you and I are watching the same video :p
This is marked resolved, were you able to find a solution outside of what was recommended above?
 

Pharma

New member
Joined
Jan 9, 2022
Messages
2
Programming Experience
Beginner
I'm assuming you and I are watching the same video :p
This is marked resolved, were you able to find a solution outside of what was recommended above?
For anyone looking for solutions to this, I followed the steps on this youtube comment. After following these steps I am able to see Vector2 and others auto complete/suggest.
1641768604772.png
 
Top Bottom