I have been looking everywhere in the code, but still don’t know where the error is.
I know it a simple code, but I’m a noobie lol
All the errors:
(32,5): error CS1519: Invalid token ‘{’ in class, struct, or interface member declaration
(33,21): error CS1519: Invalid token ‘=’ in class, struct, or interface member declaration
(35,1): error CS1022: Type or namespace definition, or end-of-file expected
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
private Vector2 movedirection;
void Update()
{
Input();
}
void FixedUpdate()
{
Move();
}
void Input()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
movedirection = new Vector2(moveX, moveY).normalized;
}
void Move();
{
rb.velocity = new Vector2(movedirection.x* speed, movedirection.y* speed);
}
}
On line 31, you put a semicolon after your declaration of void Move(). That makes the computer think that the declaration was over, so by the time it encountered the function body, it was looking for the next function instead.
tysm
but now another error pop up, “(25,23): error CS0119: ‘PlayerMovement.Input()’ is a method, which is not valid in the given context”. What does that mean?
Usually “Input” refers to a special Unity thing. But since you defined your own thing named “Input” inside of your class, the computer assumes that’s the one you’re talking about (when inside of that class).
Either change your “Input” function to have a different name, or else spell out the full name “UnityEngine.Input” when you want the Unity version.
Ty for the clear explanation, I changed Input() to Inputs() and now there no error showing up, but when I click play, the character go in the direction top left when I didn’t even press any button. (sorry for asking too many questions)
Might be time to start adding Debug.Log statements to narrow down exactly when things go wrong.
Could be that your input device is sending a signal that isn’t 100% exactly zero. Since you normalize the input vector, even a tiny input will produce the same movement as maximum input. That still shouldn’t happen with keyboards–I don’t think–but if you’ve got a gamepad with an analog stick plugged in, I wouldn’t be surprised if that’s causing issues.
Could be that something is going wrong with the normalization itself. Unity documentation says that if the vector is “too small to normalize” then a zero vector should be returned, so normalizing zero should give you zero, but I’ve never personally tested that, that I can recall.
Could be that the player is being moved by something other than this script.