I am trying to make a simple movement script with jumping in c# but ik keep getting this error and don’t know what to do:
Assets\Scripts\PlayerMovement.cs(4,11): error CS0116: A namespace cannot directly contain members such as fields or methods
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Rigidbody rb;
public class PlayerMovement : MonoBehaviour
{
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
float movementSpeed;
void Update()
{
float amountToMove = movementSpeed * Time.deltaTime;
Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
rb.AddForce(movement, ForceMode.Force);
if (Input.GetKeyDown("space"))
{
rb.AddForce(Vector3.up * jumpSpeed);
}
}
void OnCollisionStay()
{
onGround = true;
}
[SerializeField]
Vector3 v3Force;
[SerializeField]
KeyCode keyPositive;
[SerializeField]
KeyCode keyNegative;
void FixedUpdate()
{
if (Input.GetKey(keyPositive))
GetComponent<Rigidbody>().velocity += v3Force;
if (Input.GetKey(keyNegative))
GetComponent<Rigidbody>().velocity -= v3Force;
}
}