Home > Back-end >  Vector2 does not contain a definition (Unity Roll A Ball Tutorial)
Vector2 does not contain a definition (Unity Roll A Ball Tutorial)

Time:06-24

So I was doing the Unity Roll-A-Ball Tutorial, and I am on step 8 of moving the player. When I type all the code, it throws an error 'Vector2 does not contain a definition for 'Y' and no accessible extension method 'Y' accepting a first argument type of 'Vector2' could be found. (are you missing a using directive or an assembly reference?)

Any help would be appreciated

This is the code I have:

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

public class PlayerController : MonoBehaviour
{

    private Rigidbody rb;
    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void OnMove(InputValue movementValue) 
    {
        //Function Body
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.X;
        movementY = movementVector.Y;
    }

    void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement);

    }
}

CodePudding user response:

Field names in Vector2, Vector3 are lowercase x, y, z.

You can click (set the carriage position) on string that represents type (like "Vector2") in your IDE and press F12 to see what is inside or just write:

movementVector.

and wait for suggestions from IDE. Press Ctrl spacebar if they are not showing up.

  • Related